[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

Using System.Drawing to Size Images and Save Them

Brian Reed

10/18/2004 8:29:00 PM

I have a digital camera and would like to write a simple app to take
specified images from the hard disk and create web sized images that could be
posted on a web site. Currently I am using Photoshop to shrink the images
and then save them manually one by one to another location. I am hoping to
take 1600 x 1200 images and scale them programatically to 400 by 300 (or
smaller).

I was thinking of using the Image.GetThumbnailImage, but the documentation
states that this is probably not a good idea for images over 120 x 120 as the
code will programmatically stretch the inserted thumbnail image to the larger
size. I tested this and agree that the GetThumbnailImage method does not
return a good quality image at say 400 by 300.

Is there another class that could be used to perform this conversion for me?
Is there a better way to maket his conversion?
3 Answers

Morten Wennevik [C# MVP]

10/18/2004 8:58:00 PM

0

Hi Brian,

Well, you could create a bitmap of the 1600x1200 original image. Then a
blank bitmap at 400x300. Traverse every pixel and set every 4th pixel in
the blank bitmap.

I have no idea what the results will be, but you should be able to see at
least a rough thumbnail. An even better way would be to take blocks of
4x4 pixels and use the average in the blank bitmap.

--
Happy Coding!
Morten Wennevik [C# MVP]

Bob Powell

10/18/2004 11:17:00 PM

0

Pick an image size and draw the original onto that size:

Bitmap bm=(Bitmap)Bitmap.FromImage("original.jpg");
Bitmap copy=new Bitmap(400,300);
Graphics g=Graphics.FromImage(copy);
g.DrawImage(bm, new
Rectangle(0,0,400,300),0,0,bm.Width,bm.Height,GraphicsUnit.Pixel);
g.Dispose();
copy.Save("ACopy.jpg",ImageFormat.Jpeg);

You could also pick a particular dimension, say width of 400, and scale the
Y size of the copy accordingly so that the thumbnail width was always 400
regardless of the aspect ratio of the original.

If you want really nice thumbnails set the interpolation mode to
high-quality. If you're processing lots (thousands??) of images then set it
to nearest-neighbour to increase throughput at the expense of quality.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdipl...

The GDI+ FAQ RSS feed: http://www.bobpowell.net/f...
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tips...
Bob's Blog: http://bobpowelldotnet.blogspot.co...






"Brian Reed" <BrianReed@discussions.microsoft.com> wrote in message
news:6CF7D884-6947-4C76-A408-F912FDD37381@microsoft.com...
> I have a digital camera and would like to write a simple app to take
> specified images from the hard disk and create web sized images that could
be
> posted on a web site. Currently I am using Photoshop to shrink the images
> and then save them manually one by one to another location. I am hoping
to
> take 1600 x 1200 images and scale them programatically to 400 by 300 (or
> smaller).
>
> I was thinking of using the Image.GetThumbnailImage, but the documentation
> states that this is probably not a good idea for images over 120 x 120 as
the
> code will programmatically stretch the inserted thumbnail image to the
larger
> size. I tested this and agree that the GetThumbnailImage method does not
> return a good quality image at say 400 by 300.
>
> Is there another class that could be used to perform this conversion for
me?
> Is there a better way to maket his conversion?


Brian Reed

10/20/2004 4:07:00 PM

0

Thanks for the response. I appreciate it!

"Bob Powell [MVP]" wrote:

> Pick an image size and draw the original onto that size:
>
> Bitmap bm=(Bitmap)Bitmap.FromImage("original.jpg");
> Bitmap copy=new Bitmap(400,300);
> Graphics g=Graphics.FromImage(copy);
> g.DrawImage(bm, new
> Rectangle(0,0,400,300),0,0,bm.Width,bm.Height,GraphicsUnit.Pixel);
> g.Dispose();
> copy.Save("ACopy.jpg",ImageFormat.Jpeg);
>
> You could also pick a particular dimension, say width of 400, and scale the
> Y size of the copy accordingly so that the thumbnail width was always 400
> regardless of the aspect ratio of the original.
>
> If you want really nice thumbnails set the interpolation mode to
> high-quality. If you're processing lots (thousands??) of images then set it
> to nearest-neighbour to increase throughput at the expense of quality.
>
> --
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdipl...
>
> The GDI+ FAQ RSS feed: http://www.bobpowell.net/f...
> Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tips...
> Bob's Blog: http://bobpowelldotnet.blogspot.co...
>
>
>
>
>
>
> "Brian Reed" <BrianReed@discussions.microsoft.com> wrote in message
> news:6CF7D884-6947-4C76-A408-F912FDD37381@microsoft.com...
> > I have a digital camera and would like to write a simple app to take
> > specified images from the hard disk and create web sized images that could
> be
> > posted on a web site. Currently I am using Photoshop to shrink the images
> > and then save them manually one by one to another location. I am hoping
> to
> > take 1600 x 1200 images and scale them programatically to 400 by 300 (or
> > smaller).
> >
> > I was thinking of using the Image.GetThumbnailImage, but the documentation
> > states that this is probably not a good idea for images over 120 x 120 as
> the
> > code will programmatically stretch the inserted thumbnail image to the
> larger
> > size. I tested this and agree that the GetThumbnailImage method does not
> > return a good quality image at say 400 by 300.
> >
> > Is there another class that could be used to perform this conversion for
> me?
> > Is there a better way to maket his conversion?
>
>
>