473,467 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Image resize

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
3 17141
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jay | last post by:
Hi guys, trying to fix a problem with an image resize routine. The code posted below uploads and resizes a jpeg, probablem is, the outlook can look a bit bitty becuase of teh samller size...is...
3
by: Paul Jaeger | last post by:
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...
4
by: vunet.us | last post by:
Hello, What is the best image resizing tool is out there? I want to resize images in ASP page when users upload them into some directory. Your recommendations are super welcome. Thank you.
12
by: Shawn Northrop | last post by:
Ive been searching for an image resize tutorial for a while now and found this code which worked nicely. I was unable to find the full source code but i think i pieced together the code from the...
2
by: Tim Arnold | last post by:
Hi, I'm using the Image module to resize PNG images from 300 to 100dpi for use in HTML pages, but I'm losing some vertical and horizontal lines in the images (usually images of x-y plots). ...
2
by: Dominic Vella | last post by:
Hi, I know I seem to have the really complicated questions, but I guess that's why I'm here. This is a little verbose, only because I've been trying to crack this for a week now. Your help...
8
by: infoseekar | last post by:
Image Resize & Rotation Hi I have 2 scripts, one for Image rotation and other image resize and they both are working. Image resize scripts load the picture and resize it and Image rotation...
2
by: Noorain | last post by:
Hi, another problem. i upload width=800 pixels image in database through. this image resize by thumb image & bis image. thumb image width is 100 pixel & big image width is 400 pixel. 800 pixel image...
22
by: simon2x1 | last post by:
i have an image which width is 213 and height is 200 when i echo the image and i resize it echo "<img src='company/$present' width='70' height='68'/>"; the image was not as clear as when it was...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.