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

x
?
Solved

Visual Basic 2008 Resize jpg

Posted on 2011-02-15
5
Medium Priority
?
603 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

Technology Partners: 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

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