473,418 Members | 2,064 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,418 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 19048
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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
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...

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.