After searching, I’ve discovered this code:
Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)
Dim image__1 = System.Drawing.Image.FromStream(fromStream)
Dim newWidth = CInt(image__1.Width * scaleFactor)
Dim newHeight = CInt(image__1.Height * scaleFactor)
Dim thumbnailBitmap = New System.Drawing.Bitmap(newWidth, newHeight)
Dim thumbnailGraph = System.Drawing.Graphics.FromImage(thumbnailBitmap)
thumbnailGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
thumbnailGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
thumbnailGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
Dim imageRectangle = New System.Drawing.Rectangle(0, 0, newWidth, newHeight)
thumbnailGraph.DrawImage(image__1, imageRectangle)
thumbnailBitmap.Save(toStream, image__1.RawFormat)
thumbnailGraph.Dispose()
thumbnailBitmap.Dispose()
image__1.Dispose()
End Sub
There are 2 things I can’t “modify” to solve my problem:
- I wouldn’t like to pass a stream, but I prefer to pass a path like
C:\mysite\photo\myphoto.gif. How can I “convert” it to accept a file and not a stream? - In this function I’ve to pass a “scale” value. But I prefer to check if the image is too big (for example >
1024x768) than resize it to a max of1024x768. How can I check this withSystem.Drawing.
As you can see I don’t know anything about System.Drawing so I need an “hard” help to solve this job.
Here is some
c#code I did about 5 years ago to do this (it should still work I hope as the app hasn’t been touched since). I think it does everthing you need but it doesn’t upscale the image to 1024×768 if it is smaller. This code will only make sure that if it is larger than 1024×768, it will resize proportionally to fit within those dimensions:If you need to convert this to VB.NET, you can use the C# to VB.NET converter here.