Link to home
Start Free TrialLog in
Avatar of mpdillon
mpdillon

asked on

Visual Basic 2008 Resize jpg

I would like to open a .jpg image file and resize it. I have seen some code but that seems to be for bitmap images, .bmp. Before i resize the .jpg, I need to know the original Height and Width.
I want the final image to be no more than 200 pixels on its longest side. Once i know the original dimension, I can factor both the height and width before making the resize request.
I have not written any code like this before so please be specific in your reply.

thanks,
pat
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I believe that you need something that resizes, maintaining the aspect ratio, like in this snippet:

C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height
http://snippets.dzone.com/posts/show/4336

C#:

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
	System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

	// Prevent using images internal thumbnail
	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

	if (OnlyResizeIfWider)
	{
		if (FullsizeImage.Width <= NewWidth)
		{
			NewWidth = FullsizeImage.Width;
		}
	}

	int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
	if (NewHeight > MaxHeight)
	{
		// Resize with height instead
		NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
		NewHeight = MaxHeight;
	}

	System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

	// Clear handle to original file so that we can overwrite it if necessary
	FullsizeImage.Dispose();

	// Save resized picture
	NewImage.Save(NewFile);
}

Open in new window


VB.NET:

Public Sub ResizeImage(OriginalFile As String, NewFile As String, NewWidth As Integer, MaxHeight As Integer, OnlyResizeIfWider As Boolean)
	Dim FullsizeImage As System.Drawing.Image = System.Drawing.Image.FromFile(OriginalFile)

	' Prevent using images internal thumbnail
	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone)
	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone)

	If OnlyResizeIfWider Then
		If FullsizeImage.Width <= NewWidth Then
			NewWidth = FullsizeImage.Width
		End If
	End If

	Dim NewHeight As Integer = FullsizeImage.Height * NewWidth \ FullsizeImage.Width
	If NewHeight > MaxHeight Then
		' Resize with height instead
		NewWidth = FullsizeImage.Width * MaxHeight \ FullsizeImage.Height
		NewHeight = MaxHeight
	End If

	Dim NewImage As System.Drawing.Image = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, Nothing, IntPtr.Zero)

	' Clear handle to original file so that we can overwrite it if necessary
	FullsizeImage.Dispose()

	' Save resized picture
	NewImage.Save(NewFile)
End Sub

Open in new window

Avatar of mpdillon
mpdillon

ASKER

This looks like it will work. There are a few things I do not understand.
Why is the image Rotated twice? The first two lines of code.

You use the GetThumbnail method to resize the image. But if a thumbnail exists it scales the thumbnail and not the original image. I am concerned about the quality of a scaled thumbnail. Your comments suggest that by rotating the image, the thumbnail will not be used. But I do not understand.

Lastly, I am used to Photoshop where I always specify a Interpolation method when resizing either up or down. I found the same property in MSDN but I do not see how to apply it in your code. Could you please help?
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode.aspx

thanks,
pat
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you