473,441 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Resizing Drawing.Image then saving it

Hey, got a prob which is driving me nuts!

I'm trying to resize the resolution of an image as well as it's pyhsical byte size.

I've got:

byte[] bytImage = null;
System.Drawing.Image imgImage = null;
System.Drawing.Image imgNewImage = null;

using(FileStream fs = File.OpenRead(@"C:\Images\Test.jpg"))
{
bytImage = new byte[fs.Length];
fs.Read(bytImage, 0, (int)fs.Length);
imgImage = System.Drawing.Image.FromStream(fs);
fs.Close();
}

imgNewImage = new Bitmap(imgImage, 200, 200);

imgNewImage.Save(@"C:\Images\Test_1.jpg"))

What's goin wrong is this:
Test.jpg is originally 400 x 600, with PixelFormat = Format24bppRgb
Test_1.jpg is 200 x 200 BUT with PixelFormat = Format32bppARgb

So I tend to get my resized images file bigger in byte size than the original!

:( How do I rectify this? Or is there a better alternative to resizing an Image?
Nov 16 '05 #1
4 19053
Have you tried
imgNewImage = new Bitmap(imgImage, 200, 200,PixelFormat.Format24bppRgb);

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
This is the code I wrote to get it to work. My original file
(Test.jpg) was a 640 * 480 file of size 82.4KB. After running my code,
my new file (Test10.jpg) was a 200 * 200 file of size 6.62KB.

/*
* Created by SharpDevelop.
* User: Sushant
* Date: 11/12/2004
* Time: 9:23 AM
*
* To change this template use Tools | Options | Coding | Edit
Standard Headers.
*/

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

class MainClass
{
public static int Main(string[] args)
{
Image oldImage = Image.FromFile(@"Test.jpg");
Image thumb = new Bitmap(200, 200,
System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
Graphics oGraphic = Graphics.FromImage(thumb);

oGraphic.CompositingQuality = CompositingQuality.HighSpeed;
oGraphic.SmoothingMode = SmoothingMode.HighSpeed;
oGraphic.InterpolationMode = InterpolationMode.Low;

Rectangle rect = new Rectangle(0,0, 200, 200);
oGraphic.DrawImage(oldImage, rect);
thumb.Save("Test10.jpg", ImageFormat.Jpeg);

return 0;
}
}
Nov 16 '05 #3
Thanks for your help!

Shushant, your code was really helpful!

One more question with regards to this topic. Shushant, maybe you can
help me with this again!

If I want to resize a Gif:

Image oNewImage = new Bitmap(200, 200,
System.Drawing.Imaging.PixelFormat.Format8bppIndex ed);

Graphics oGraphics = Graphics.FromImage(oNewImage);

I would get an error saying something like
"Graphics object canont be created from an image that has an indexed
pixel format"

:( how now?
Nov 16 '05 #4
Hi Luanne

I tried my code with a gif file and i got no error. The only changes
I made was to the name of the file. For instance, the line

Image oldImage = Image.FromFile(@"Test.jpg");

was changed to

Image oldImage = Image.FromFile(@"test.gif");

Notice that the file name is changed from Test.jpg to test.gif.

So I started with a gif file 316 * 207 at 12.7KB and got a JPG file of
200 * 200 at 8.72KB.

Seems to work for me with the modification to the file name. Did u
remember to change that?
Just to be sure, I posted the code again with the change I made. Let
me know if there is anything else? Perhaps you can post a link to the
image files your using. That way I can test it out.


using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

class MainClass
{
public static int Main(string[] args)
{
Image oldImage = Image.FromFile(@"test.gif");
Image thumb = new Bitmap(200, 200,
System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
Graphics oGraphic = Graphics.FromImage(thumb);

oGraphic.CompositingQuality = CompositingQuality.HighSpeed;
oGraphic.SmoothingMode = SmoothingMode.HighSpeed;
oGraphic.InterpolationMode = InterpolationMode.Low;

Rectangle rect = new Rectangle(0,0, 200, 200);
oGraphic.DrawImage(oldImage, rect);
thumb.Save("Test10.jpg", ImageFormat.Jpeg);

return 0;
}
}
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
2
by: Brad | last post by:
I have code which takes an image, uploaded from a web page, and saves it to a database. Now I want to always resize an uploaded image before it is saved to the database. My code to resize is...
2
by: Jeremy | last post by:
I am creating an imaging service using WIA (Windows Imaging Acquisition). The problem is that I cannot convert a WIA.ImageFile to a System.Drawing.Image to put a timestamp on the image without...
4
by: Darrel | last post by:
I'm grabbing a file from a file upload form field. This is a 'system.web.httppostedfile' I would like to modify the image (Cropping/scaling) using system.drawing.image. Is there anyway to go...
0
by: byrd48 | last post by:
Hi, I have a method that creates a system.drawing.image behind the scenes. I would like to load this image to a web page as a control, as in td.controls.add(). I know I can save this generated...
5
by: Jerry J | last post by:
I want to use the System.Drawing.Image class. According to the help file, this is an abstract base class. Because it is supposedly abstract, I created another class that inherits from it. However,...
1
by: Blasting Cap | last post by:
I would like to try to see if an image uploaded to a SQL database is over a specific size, and if so, to resize it when it is displayed. The following is in the Page_load event. Dim strImageID...
3
by: Dave Keen | last post by:
Hi all. Hope you can help me. This should be easy but I can't make this work. In brief I am building a page of thumbnails using images held in a SQLServer 2000 database. I do this by creating...
0
by: Eyadtrabulsi | last post by:
Hi, I have a method that creates a system.drawing.image behind the scenes. I would like to load this image to a web page as a control, as in td.controls.add(). I know I can save this generated...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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: 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...

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.