?
Solved

Visual Basic 2008 Resize jpg

Posted on 2011-02-15
5
Medium Priority
?
596 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:mpdillon
[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
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34903180
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

0
 

Author Comment

by:mpdillon
ID: 34903380
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
0
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 34903543
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 34907616
Why is the image Rotated twice?

That is a great question, since the comment is "Prevent using images internal thumbnail", but I don't know how that helps, and what the problem is with using the internal thumbnail.

InterpolationMode is part of the Graphics class.  There is a link at the bottom of that referenced MSDN article.

How to: Use Interpolation Mode to Control Image Quality During Scaling
http://msdn.microsoft.com/en-us/library/k0fsyd4e.aspx

        Dim image As New Bitmap("GrapeBunch.bmp")
        Dim width As Integer = image.Width
        Dim height As Integer = image.Height

        ' Draw the image with no shrinking or stretching. Pass in the destination
        ' rectangle (2nd argument), the upper-left corner (3rd and 4th arguments),
        ' width (5th argument),  and height (6th argument) of the source 
        ' rectangle.
        e.Graphics.DrawImage( _
            image, _
            New Rectangle(10, 10, width, height), _
            0, _
            0, _
            width, _
            height, _
            GraphicsUnit.Pixel, _
            Nothing)

        ' Shrink the image using low-quality interpolation. 
        e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor

        ' Pass in the destination rectangle, and the upper-left corner, width, 
        ' and height of the source rectangle as above.
        e.Graphics.DrawImage( _
        image, _
        New Rectangle(10, 250, CInt(0.6 * width), CInt(0.6 * height)), _
        0, _
        0, _
        width, _
        height, _
        GraphicsUnit.Pixel)

        ' Shrink the image using medium-quality interpolation.
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear

        ' Pass in the destination rectangle, and the upper-left corner, width, 
        ' and height of the source rectangle as above.
        e.Graphics.DrawImage( _
        image, _
        New Rectangle(150, 250, CInt(0.6 * width), _
        CInt(0.6 * height)), _
        0, _
        0, _
        width, _
        height, _
        GraphicsUnit.Pixel)

        ' Shrink the image using high-quality interpolation.
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic

        ' Pass in the destination rectangle, and the upper-left corner, width, 
        ' and height of the source rectangle as above.
        e.Graphics.DrawImage( _
            image, _
            New Rectangle(290, 250, CInt(0.6 * width), CInt(0.6 * height)), _
            0, _
            0, _
            width, _
            height, _
            GraphicsUnit.Pixel)

Open in new window

0
 

Author Closing Comment

by:mpdillon
ID: 35026003
Thank you
0

Featured Post

Independent Software Vendors: 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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…
Suggested Courses

765 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