Solved

vb.net - resize images

Posted on 2013-06-13
1
348 Views
Last Modified: 2013-06-13
hello there,
I am using this code to re-size small images into a bigger size.
the Issue that I am having is that it stretches the images and I would like to re-size but keep the aspect ratio so that it doesn't stretch out.. how can I do this?

    Public Sub ResizeImage(ByVal fileName As String, ByVal width As Integer, height As Integer)
        Dim bm As New Bitmap(fileName)

        Dim thumb As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(thumb)

        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(bm, New Rectangle(0, 0, width, height), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
        g.Dispose()
        bm.Dispose()

        thumb.Save(fileName & "new.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        thumb.Dispose()

    End Sub

Open in new window

0
Comment
Question by:XK8ER
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39243744
Public Sub FixedSize(fileName As String, Width As Integer, Height As Integer) As Image
	Dim imgPhoto As Image = Image.FromFile(fileName)
	Dim sourceWidth As Integer = imgPhoto.Width
	Dim sourceHeight As Integer = imgPhoto.Height
	Dim sourceX As Integer = 0
	Dim sourceY As Integer = 0
	Dim destX As Integer = 0
	Dim destY As Integer = 0

	Dim nPercent As Single = 0
	Dim nPercentW As Single = 0
	Dim nPercentH As Single = 0

	nPercentW = (CSng(Width) / CSng(sourceWidth))
	nPercentH = (CSng(Height) / CSng(sourceHeight))
	If nPercentH < nPercentW Then
		nPercent = nPercentH
		destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2)
	Else
		nPercent = nPercentW
		destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2)
	End If

	Dim destWidth As Integer = CInt(Math.Truncate(sourceWidth * nPercent))
	Dim destHeight As Integer = CInt(Math.Truncate(sourceHeight * nPercent))

	Dim bmPhoto As New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
	bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)

	Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
	grPhoto.Clear(Color.Red)
	grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

	grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)

	grPhoto.Dispose()
	bmPhoto.Save(fileName)
End Function

Open in new window

from http://stackoverflow.com/questions/1940581/c-sharp-image-resizing-to-different-size-while-preserving-aspect-ratio
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now