Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear fellows, I have been searching for hours for this solution.
I successfully create jpeg file from panel with this code:
Bitmap panelImage = new Bitmap(mapPanel.Width, mapPanel.Height);

Graphics g = Graphics.FromImage((Image)panelImage);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

mapPanel.DrawToBitmap(panelImage, mapPanel.ClientRectangle);

g.DrawImage(panelImage, 0, 0, mapPanel.Width, mapPanel.Height);
g.Dispose();

panelImage.Save(saveImageDialog.FileName, ImageFormat.Jpeg);


the output is as below (the resolution is bad)

http://e1302.hizliresim.com/16/5/jtpjy.jpg

then I tried to make the resolution higer using this code:
Bitmap panelImage = new Bitmap(3508, 2480);
panelImage.SetResolution(300, 300);

Graphics g = Graphics.FromImage((Image)panelImage);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

mapPanel.DrawToBitmap(panelImage, mapPanel.ClientRectangle);

g.DrawImage(panelImage, 0, 0, 3508, 2480);
g.Dispose();

panelImage.Save(saveImageDialog.FileName, ImageFormat.Jpeg);


but it resulted like this:
http://e1302.hizliresim.com/16/5/jtny7.jpg

the panel area did not fill the whole page.

please help me

thank you
Posted
Updated 5-Feb-13 10:53am
v5

1 solution

Bitmap.SetResolution does not increase resolution. This term is misleading. The change in this parameter does not change anything in the presentation of the bitmap, in its content. It only fills in metadata showing how many dots per inch does it have by default. In other words, it defines some default scaling of the image, which can be used, say, for printing. Please see:
Record User Activity[^].

You want to increase the size of the bitmap in pixels. You are not even trying to do it: your bitmap is 3508x2480, and then you try to draw it as 3508x2480. Nothing changes. But if you really up-scaled the image, the quality of the result could be very poor. Actually, you cannot effectively enlarge the image by a considerable factor with good quality. More exactly, there are some "intellectual" algorithms for doing such things, such as fractal compression (http://en.wikipedia.org/wiki/Fractal_compression[^]; yes, compression, in that sense, can help to enlarge images with better quality), but the results are still limited. And this is called "re-sampling".

Even though the size of the bitmap in pixels is often called "resolution" and is related to resolution, in fact, this stuff has nothing to do with "resolution". Resolution is the ability to resolve two separate features as separate in imagine. Let's say, if you have two closely located features of some object, and then make an image with certain lens and camera, these two features can look blurred together or distinct. Resolution is usually measured in lines per mm (or inch) and characterized the ability to reproduce some sample pattern, usually a periodic pattern of contrast straight lines…

—SA
 
Share this answer
 
Comments
Ace Supriatna 6-Feb-13 1:29am    
Thank you Mr. Sergey Alexandrovich,

Your explanation really enlightened me.
Nevertheless, I would still like to save a panel, which is in A4 size (595 x 842).
Is there a way to save it to a better quality. at least the output is as the same as the input image (which is always in a better quality)

Thanks
Sergey Alexandrovich Kryukov 6-Feb-13 1:47am    
Just provide output size you want in DrawImage. If you scale down, the qualify will be good (and even just a bit up), but not if you scale up considerably. You cannot really improve quality, because you cannot create more information than you have. If you original image is vector, you can scale vector first, and then rasterize it.

The procedure is very simple, for example:
http://stackoverflow.com/questions/87753/resizing-an-image-without-losing-any-quality
(I thought you already knew it basically...)

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900