Note that there are some explanatory texts on larger screens.

plurals
  1. POimage resize and crop on upload asp,net
    primarykey
    data
    text
    <p>i have the below code, i am looking to crop and resize at the same time, i use the below function, i am looking to have 150 x 150 px image size cropped centered, but the below function always if the image width > height, the output image will be 200x150, but i need it to be 150x150 px,, any help</p> <pre><code>Function SavetoDisk(FU As FileUpload, ByVal para_Save_to_Path As String, ByVal maxHeight As Integer, ByVal maxWidth As Integer, para_FileExt As String, anchor As AnchorPosition) Using image As Image = image.FromStream(FU.PostedFile.InputStream) Dim sourceWidth As Integer = image.Width Dim sourceHeight As Integer = image.Height Dim sourceX As Integer = 0 Dim sourceY As Integer = 0 Dim destX As Integer = 0 Dim destY As Integer = 0 Dim nPercent As Decimal = 0 Dim nPercentW As Decimal = 0 Dim nPercentH As Decimal = 0 nPercentW = (Convert.ToSingle(maxWidth) / Convert.ToSingle(sourceWidth)) nPercentH = (Convert.ToSingle(maxHeight) / Convert.ToSingle(sourceHeight)) If (nPercentH &lt; nPercentW) Then nPercent = nPercentW Select Case (anchor) Case AnchorPosition.Top destY = 0 Case AnchorPosition.Bottom destY = Convert.ToInt32(maxHeight - (sourceHeight * nPercent)) Case Else destY = Convert.ToInt32((maxHeight - (sourceHeight * nPercent)) / 2) End Select Else nPercent = nPercentH Select Case (anchor) Case AnchorPosition.Left destX = 0 Case AnchorPosition.Right destX = Convert.ToInt32((maxWidth - (sourceWidth * nPercent))) Case Else destX = Convert.ToInt32(((maxWidth - (sourceWidth * nPercent)) / 2)) End Select End If Dim destWidth As Integer = Convert.ToInt32((sourceWidth * nPercent)) Dim destHeight As Integer = Convert.ToInt32((sourceHeight * nPercent)) Using thumbnailBitmap As Bitmap = New Bitmap(destWidth, destHeight) Using thumbnailGraph As Graphics = Graphics.FromImage(thumbnailBitmap) thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic Dim imageRectangle As Rectangle = New Rectangle(0, 0, destHeight, destHeight) thumbnailGraph.DrawImage(image, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel) Dim jpegCodec As ImageCodecInfo = Findcodecinfo("JPEG") If Not IsNothing(jpegCodec) Then Dim encoderParameters As EncoderParameters = New EncoderParameters(1) encoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 80) thumbnailBitmap.Save(para_Save_to_Path, jpegCodec, encoderParameters) Else thumbnailBitmap.Save(para_Save_to_Path, ImageFormat.Jpeg) End If End Using End Using End Using End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload