Image resize

  • Thread starter Thread starter Paul Jaeger
  • Start date Start date
P

Paul Jaeger

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.
 
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.
 
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.
 
Back
Top