By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

Image resize

P: n/a
I have jpeg images that are 2848 x 4256 pixels. I want to programatically
convert them to images that are approximately 427 x 638 (that maintains the
ratio) and save to new jpeg files.

How can .net accomplish the image resizing?

Paul J.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Follow this link:
http://microsoft.com/downloads/detai...displaylang=en

Download the 101 VBNET examples and in it you will find example for working
with images in GDI+, using VB.NET.
There is one sample there that demonstrates exactly what you want to do.
drop me an email if you have problems with it.
james

"Paul Jaeger" <pa***@aya.yale.edu> wrote in message
news:uk*********************@bgtnsc05-news.ops.worldnet.att.net...
I have jpeg images that are 2848 x 4256 pixels. I want to programatically
convert them to images that are approximately 427 x 638 (that maintains the ratio) and save to new jpeg files.

How can .net accomplish the image resizing?

Paul J.

Jul 19 '05 #2

P: n/a
Hi,

Hello Paul,

You could use GDI+ to achieve it. Please refer to the following sample:

//----------code snippet-------------------
Bitmap bitmap = new Bitmap(file);

// create the output bitmap
Bitmap newBitmap = new Bitmap(widthToUse, heightToUse);

// create a Graphics object to draw into it
Graphics g = Graphics.FromImage(newBitmap);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

// draw it with the new size
g.DrawImage(bitmap, 0, 0, widthToUse, heightToUse);

// save the bitmap using the JPG encoder
newBitmap.Save(saveFilename, myImageCodecInfo, myEncoderParameters);
//-----------end of-----------------------------

Hope it helps.

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '05 #3

P: n/a
The save is not that hard. There are two ways to fix it, one, after opening
the original image file you want to resize, copy it to another Bitmap and
resize the second version.
Dispose the first one and save the second one.
If you use the FromFile method in the example, it holds the file open as
long as the image is in memory. That is why you copy it to another bitmap
and then dispose of the first one to release the file. Then when you save
the image, using the same Filename it will overwrite the original file with
the new image size and different file size.
The second method is to simply save the resized image with a different file
name.
james
Here's some code to give you the idea:
Private Sub SaveResized_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles SaveResized.Click

Dim bm As New Bitmap(PictureBox1.Image)

Dim myX As Integer

Dim myY As Integer

myX = Val(txtX.Text)

myY = Val(txtY.Text)

If Val(txtY.Text) = 0 Or Val(txtX.Text) = 0 Then Exit Sub Else

Dim thumb As New Bitmap(myX, myY)

Dim g As Graphics = Graphics.FromImage(thumb)

g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

g.DrawImage(bm, New Rectangle(0, 0, myX, myY), New Rectangle(0, 0, bm.Width,
bm.Height), GraphicsUnit.Pixel)

g.Dispose()

Dim message, title, defaultValue As String

Dim myValue As String

message = "Enter File Name:" ' Set prompt.

title = "Save Resized Photo" ' Set title.

defaultValue = "" ' Set default value.

'get name to save resized image, using Inputbox

myValue = InputBox(message, title, defaultValue)

If myValue = "" Then bm.Dispose() : thumb.Dispose() : Exit Sub Else

thumb.Save("C:\" + myValue + ".jpg",
System.Drawing.Imaging.ImageFormat.Jpeg)

bm.Dispose()

thumb.Dispose()

Dim caption As String = "Save Resized Photo"

MessageBox.Show("The Resized Photo has been saved as: " & myValue, caption,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

End Sub

Just add a couple of textboxes to a form, name one txtX and the other txtY.

A button named SaveResized. (this assumes you have a Picturebox on the
form.)

Then when you run the code just enter the Width and Height you want into
each textbox and click on the SaveResized button and you will get an
Inputbox that pops up and asks for a file name to save the resized image to.
Enter just the name and Click OK. It will save the new image using whatever
name you want, including the original name. It will overwrite the original
file with the changed sizes too.
"Paul Jaeger" <pa***@aya.yale.edu> wrote in message
news:xy*********************@bgtnsc05-news.ops.worldnet.att.net...
The save seems to be the hard part. No matter what the size of the image on screen, the size of the resulting file is the same.

Anyone else?
"james" <jj*******@earthlink.net> wrote in message
news:ej**************@TK2MSFTNGP11.phx.gbl...
Follow this link:

http://microsoft.com/downloads/detai...5AEB-4F46-9BF0 -2B3E3664BE77&displaylang=en

Download the 101 VBNET examples and in it you will find example for

working
with images in GDI+, using VB.NET.
There is one sample there that demonstrates exactly what you want to do.
drop me an email if you have problems with it.
james

"Paul Jaeger" <pa***@aya.yale.edu> wrote in message
news:uk*********************@bgtnsc05-news.ops.worldnet.att.net...
I have jpeg images that are 2848 x 4256 pixels. I want to programatically convert them to images that are approximately 427 x 638 (that
maintains the
ratio) and save to new jpeg files.

How can .net accomplish the image resizing?

Paul J.



Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.