Solved

vb.net - resize images

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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