[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

vb.net - resize images

Posted on 2013-06-13
1
Medium Priority
?
361 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:
Meir Rivkin 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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