Solved

vb.net - resize images

Posted on 2013-06-13
1
347 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

19 Experts available now in Live!

Get 1:1 Help Now