?
Solved

vb.net - resize images

Posted on 2013-06-13
1
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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