473,508 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ 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 17146
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
2283
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
354
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
3000
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
3335
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
2487
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
20875
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
9375
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
2027
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
4934
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...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
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,...
0
4705
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
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
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 ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.