Asked by:
Resize Photo VB.NET

Question
-
User1158659262 posted
Hello,
I'm using this code to resize photos. It resizes the photos fine. I'm resizing to 100px. The problem is that when resized, if the image is wide, it creates 17px of white space on the top and bottom of the photo. If the photo is tall, it makes 17px of white space on the left and right of the photo. The white space is always consistant. Is there anything I can add to the code to get rid of this white space? Or maybe someway I can crop the photo down to get rid of the white space? Thank you.
newg.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
newg.Clear(Color.White)
If WidthVsHeightRatio = 1D Thennewg.DrawImage(originalBitmap, 0, 0, 100, 100)
newg.Save()
ElseIf WidthVsHeightRatio < 1D Then 'Image is taller than wider newg.DrawImage(originalBitmap, New RectangleF(New PointF((50 - (100 * WidthVsHeightRatio) / 2), 0), New SizeF(100 * WidthVsHeightRatio, 100.0F)))newg.Save()
Else 'Image is wider than taller Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1)newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, 50 - ((100 * inverse) / 2)), New SizeF(100.0F, 100 * inverse)))newg.Save()
End If End Using newbmp.Save(Server.MapPath("tn_" & fileImage.FileName), System.Drawing.Imaging.ImageFormat.Jpeg) End UsingSunday, November 16, 2008 10:34 PM
All replies
-
User-340665729 posted
newg.Clear(Color.White) - try Color.Transparent
Monday, November 17, 2008 12:43 AM -
User-512273051 posted
Well I tried it alot but due to the lack of time I tried to manege the height stuff only. Maybe it can help you to proceed this further or give me a day so that I can do that for you .
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FileToResize As String = "C:\image\agha.jpg" Using originalBitmap As Bitmap = Bitmap.FromFile(FileToResize, True), newbmp As Bitmap = New Bitmap(100, 100) Dim WidthVsHeightRatio = CDec(originalBitmap.Width) / CDec(originalBitmap.Height) Using newg As Graphics = Graphics.FromImage(newbmp) newg.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic newg.Clear(Color.White) If WidthVsHeightRatio = 1D Then newg.DrawImage(originalBitmap, 0, 0, 100, 100) newg.Save() ElseIf WidthVsHeightRatio < 1D Then 'Image is taller than wider newg.DrawImage(originalBitmap, New RectangleF(New PointF((50 - (100 * WidthVsHeightRatio) / 2), 0), New SizeF(100 * WidthVsHeightRatio, 100.0F))) newg.Save() Else 'Image is wider than taller Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1) newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, 50 - ((100 * inverse) / 2)), New SizeF(100.0F, 100 * inverse))) newg.Save() End If End Using Dim startPoint As Point = getStartXY(newbmp.Height, newbmp.Width, newbmp) Dim EndPoint As Point = GetEndXY(newbmp.Height, newbmp.Width, newbmp) Response.Write(startPoint) Response.Write(EndPoint) Dim myBmp As Bitmap = CropBitmap(newbmp, startPoint.X, startPoint.Y, (newbmp.Width - startPoint.X), EndPoint.Y) myBmp.Save(Server.MapPath("tn_agha.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg) End Using End Sub Public Function getStartXY(ByVal Height As Int32, ByVal Width As Int32, ByVal img As Bitmap) As Point Dim y As Integer For y = 0 To Height - 1 Dim x As Integer For x = 0 To Width - 1 Dim pixel As Color = img.GetPixel(x, y) If Not pixel.R = 255 And pixel.G = 255 And pixel.B = 255 Then Return New Point(x, y) End If Next Next End Function Public Function GetEndXY(ByVal Height As Int32, ByVal Width As Int32, ByVal img As Bitmap) As Point Dim y As Integer For y = Height - 1 To 0 Step -1 Dim x As Integer For x = Width - 1 To 0 Step -1 Dim pixel As Color = img.GetPixel(x, y) If Not pixel.R = 255 And pixel.G = 255 And pixel.B = 255 Then Return New Point(x, y) End If Next Next End Function Private Function CropBitmap(ByRef bmp As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight) Dim cropped As Bitmap = bmp.Clone(rect, bmp.PixelFormat) Return cropped End Function
Monday, November 17, 2008 2:12 AM -
User1158659262 posted
Sorry took so long for me to get back to you guys, I've been really busy that last few days.
I tried color.transparent but no luck.
aghausman12,
I tried to follow what you came up with but honestly, I am not good at all with the graphics class. So maybe you can help me understand a little better what I need to do with this code and what it is doing. I thank you for your help.
Tony Ricketts
Friday, November 21, 2008 12:07 AM -
User-512273051 posted
Please implement my code in your project .. that really simple. all you have to do is to copy pase it and call functions accordingly then check it ... it will atleast solve the problem for horizontal white spaces. and please be quick on this one
Friday, November 21, 2008 9:00 AM -
User1158659262 posted
I did implement your code, it does take away the white space, however it also takes away a big section of the photo. Would it matter if I'm resizing the photo twice? First I resize the original to 200x200 and save that. Then I resize the original again to 100x100 and save that. That should not make a difference as they are resized individually. Maybe I should have mentioned that before. I don't know if that will make a difference? My apologies for the trouble. Thank you for your help.
Tony Ricketts
Friday, November 21, 2008 11:21 AM -
User-512273051 posted
Well I guess you need to resize photo once.
Thursday, November 27, 2008 10:37 AM -
User170196440 posted
Hi all,
i copy page the code to my VWD2008 at default.asp.vb page.
I have problem with BITMAP
The error: "Type Bitmap is not defined"
Pls help
newg.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
newg.Clear(Color.White)
If WidthVsHeightRatio = 1D Thennewg.DrawImage(originalBitmap, 0, 0, 100, 100)
newg.Save()
ElseIf WidthVsHeightRatio < 1D Then 'Image is taller than wider newg.DrawImage(originalBitmap, New RectangleF(New PointF((50 - (100 * WidthVsHeightRatio) / 2), 0), New SizeF(100 * WidthVsHeightRatio, 100.0F)))newg.Save()
Else 'Image is wider than taller Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1)newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, 50 - ((100 * inverse) / 2)), New SizeF(100.0F, 100 * inverse)))newg.Save()
End If End UsingSaturday, February 28, 2009 7:16 AM -
User1158659262 posted
Make sure you have the imports at the top of your .vb page
Imports System.Drawing
Saturday, February 28, 2009 12:15 PM