[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.drawing

Saving an image to a stream

rnorthedge

11/19/2004 9:36:00 AM

Hello,

I am fairly new to GDI+ so forgive me if this is a simple question. I
have searched on the newsgroups and cannot find an answer.

In C# I am creating a new Bitmap object:

Bitmap oBannerImage = new Bitmap(610, 60);
Graphics oBannerGraphics = Graphics.FromImage(oBannerImage);

and then using the Graphics object to draw a load of stuff into the
image.

At the end, if I do

oBannerImage.Save("C:\banner.gif");

then I get a small (7K), good quality image file. However, I would
like to save the image data into a MemoryStream instead, to manipulate
the bytes further. Unfortunately there is no Save overload that takes
just a Stream; I need to use the overload that uses a Stream and an
ImageFormat object. But I do not know what ImageFormat object to use
to get the same results as I am getting from the Save method with the
filename alone. Can anyone tell me what format the
oBannerImage.Save("C:\banner.gif"); call uses by default, so I can
reproduce the same 7K image into a stream? I have used
ImageFormat.Gif (this gives a low quality image), ImageFormat.Bmp
(this gives a higher quality image but with a much bigger file size).

Any help greatly appreciated

Thanks,
Richard
3 Answers

Morten Wennevik [C# MVP]

11/19/2004 10:15:00 AM

0

Hi Richard,

Since you are saving as a gif file I would use

MemoryStream bannerStream = new MemoryStream();
oBannerImage.Save(bannerStream, ImageFormat.Gif);

However, the default is ImageFormat.Jpeg so even though you save it as
banner.gif it is in fact a jpeg picture. Windows doesn't really care
since it determines the correct file format by reading the image header,
but other programs might not be able to handle a jpg picture disguised as
gif.

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

rnorthedge

11/19/2004 7:39:00 PM

0

"Morten Wennevik" <MortenWennevik@hotmail.com> wrote in message news:<opshpiqs1iklbvpo@pbn_computer>...
> Hi Richard,
>
> Since you are saving as a gif file I would use
>
> MemoryStream bannerStream = new MemoryStream();
> oBannerImage.Save(bannerStream, ImageFormat.Gif);
>
> However, the default is ImageFormat.Jpeg so even though you save it as
> banner.gif it is in fact a jpeg picture. Windows doesn't really care
> since it determines the correct file format by reading the image header,
> but other programs might not be able to handle a jpg picture disguised as
> gif.

Actually, I examined the output file using a hex editor and it looks
like the default is PNG. This is probably ok for my purposes so I'll
just do that.

Cheers,

Richard

Morten Wennevik [C# MVP]

11/19/2004 8:37:00 PM

0

On 19 Nov 2004 11:39:14 -0800, Richard Northedge <rnorthedge@hotmail.com>
wrote:

> "Morten Wennevik" <MortenWennevik@hotmail.com> wrote in message
> news:<opshpiqs1iklbvpo@pbn_computer>...
>> Hi Richard,
>>
>> Since you are saving as a gif file I would use
>>
>> MemoryStream bannerStream = new MemoryStream();
>> oBannerImage.Save(bannerStream, ImageFormat.Gif);
>>
>> However, the default is ImageFormat.Jpeg so even though you save it as
>> banner.gif it is in fact a jpeg picture. Windows doesn't really care
>> since it determines the correct file format by reading the image header,
>> but other programs might not be able to handle a jpg picture disguised
>> as
>> gif.
>
> Actually, I examined the output file using a hex editor and it looks
> like the default is PNG. This is probably ok for my purposes so I'll
> just do that.
>
> Cheers,
>
> Richard

You may be right. I just compared the file size of a regularly file saved
as ?.gif with a file saved with the ImageFormat.Jpeg format, and since
both were identical ...

Then again, in my case I opened a jpg file to begin with so the jpeg
format would carry over when saved.

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