Asked by:
Image Resize Whitespace/Blackspace issue.. weird!

Question
-
User635479908 posted
Hey all,
I am using this code to resize an image to a static size, and i used to use newGraphic.Clear(Color.White), which used to leave a bunch of the sides or tops with some white space. I now use newGraphic.Clear(Color.Transparant) which acctually works fine on my local development pc, it sizes it perfectly and clears off the pixels on the side. However when i upload it to my server, it adds black where the whitespace used to be. I just dont get it! ive tried this on 3 different servers now and its the same thing!
Here's my code:
Private Sub Pic(ByVal fu As Telerik.Web.UI.RadUpload, ByVal index As UploadedFile) Const bmpW = 300 'New image target width Const bmpH = 256 'New Image target height errlabel.Text = "" Dim newWidth As Integer = bmpW Dim newHeight As Integer = bmpH Dim upName As String = DateTime.UtcNow.Ticks & ".png" Dim filePath As String = "C:\" & upName Dim upBmp As Bitmap = Bitmap.FromStream(index.InputStream) Dim newBmp As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb) newBmp.SetResolution(72, 72) Dim upWidth As Integer = upBmp.Width Dim upHeight As Integer = upBmp.Height Dim newX As Integer = 0 'Set the new top left drawing position on the image canvas Dim newY As Integer = 0 Dim reDuce As Decimal If upWidth > upHeight Then 'Landscape picture reDuce = newWidth / upWidth newHeight = Int(upHeight * reDuce) newY = Int((bmpH - newHeight) / 2) newX = 0 'Picture will be full width ElseIf upWidth < upHeight Then 'Portrait picture reDuce = newHeight / upHeight newWidth = Int(upWidth * reDuce) newX = Int((bmpW - newWidth) / 2) newY = 0 'Picture will be full hieght ElseIf upWidth = upHeight Then 'square picture reDuce = newHeight / upHeight newWidth = Int(upWidth * reDuce) newX = Int((bmpW - newWidth) / 2) 'Position the image centrally across the canvas newY = Int((bmpH - newHeight) / 2) 'Position the image centrally down the canvas End If Dim newGraphic As Graphics = Graphics.FromImage(newBmp) Try newGraphic.Clear(Color.Transparent) newGraphic.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias newGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight) newBmp.Save(filePath, Imaging.ImageFormat.Png) lblpic.Text = "/lstimages/" & upName Catch ex As Exception errlabel.Text = ex.ToString Finally upBmp.Dispose() newBmp.Dispose() newGraphic.Dispose() End Try End Sub
Monday, March 30, 2009 1:54 PM
All replies
-
User1396024914 posted
hi Mnemonic
check this out to resize image:
http://jonyshah.wordpress.com/2009/09/04/resize-image-uploaded-by-user-in-asp-net/
Don't forogt to mark as answer
Tuesday, September 29, 2009 2:42 PM -
User1176561268 posted
Hi....
You can resize the image using the following code..
string displayedImghead;
//Get the path of the original Image
displayedImghead = Server.MapPath("~/Admin/Banner/Thumbnail/") + FUHead.FileName;
// Get the path of the Thumb folder
string displayedImgThumbhead = Server.MapPath("~/Admin/Banner/");
// Get the original image file name
string imgFileNamehead = System.IO.Path.GetFileName(displayedImghead);
// Load original image
System.Drawing.Image myimghead = System.Drawing.Image.FromFile(displayedImghead);
// Get the thumbnail size px
myimghead = myimghead.GetThumbnailImage(468, 60, null, IntPtr.Zero);
// Save the new thumbnail image
myimghead.Save(displayedImgThumbhead + imgFileNamehead, myimghead.RawFormat);
Hope It Helps....Please mark as answer if useful
Thanks
Kinjal
Sharma Infoway
Friday, October 9, 2009 2:32 AM