Solved

Visual Basic 2008 Resize jpg

Posted on 2011-02-15
5
570 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
  • 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 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now