Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bitmap.Save with Encoder.Compression does not work...

175 views
Skip to first unread message

SKD

unread,
Jun 2, 2002, 7:43:12 PM6/2/02
to
Hello,

I have simple code snippet below (which is not very
different from the MSDN documentation for
Encoder.Compression (except for the image format which is
TIFF in the msdn example).
I am trying to load and paint a new image and then save
it in a new file. The original photoshop file is 1K. But
the output of this file is 12K. Please help if I am doing
something wrong here, in reducing the size of this file.
I have tried other enumerated values of Compression, but
12K is all I get always.

Thanks a lot in advance.

...........
Image iNew = Image.FromFile
("C:\\Work\\ImageConvert\\old.gif");
Bitmap bmNew = new Bitmap(iNew.Width, iNew.Height);
Graphics gNew = Graphics.FromImage(bmNew);
gNew.DrawImage(iNew, 0, 0, iNew.Width, iNew.Height);
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(new Encoder
(Encoder.Compression.Guid), (long)
EncoderValue.CompressionLZW);
bmNew.Save("C:\\Work\\ImageConvert\\new.gif", getCodecInfo
("image/gif"), ep);
.........

private ImageCodecInfo getCodecInfo(string mt)
{
ImageCodecInfo[] ici = ImageCodecInfo.GetImageEncoders();
int idx = 0;
for (int ii=0; ii<ici.Length; ii++)
{
if (ici[ii].MimeType == mt)
{
idx = ii;
break;
}
}
return ici[idx];
}


John Hornick [MS]

unread,
Jun 3, 2002, 3:40:47 PM6/3/02
to
Hi,


You're using GIF, which doesn't support that encoder parameter. You
can use Bitmap.GetEncoderParameterList() to make this determination
programmatically.

There is a topic in the PSDK GDI+ documentation titled "Determining
the Parameters Supported by an Encoder" which describes this.


Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit http://www.microsoft.com/security for current information on security.

SKD

unread,
Jun 5, 2002, 12:20:51 PM6/5/02
to
Hello John,

Thanks for the reply.
I tried this
EncoderParameters epNew = bmNew.GetEncoderParameterList
(ImageFormat.Gif.Guid);
It raises a file not found exception. It could be
something very stupid. If you can help, it will be great.

Thanks,
SKD.

SKD

unread,
Jun 5, 2002, 3:04:13 PM6/5/02
to
Hi,

Now I did this

ImageCodecInfo[] ici = ImageCodecInfo.GetImageEncoders();


for (int ii=0; ii<ici.Length; ii++)
{

EncoderParameters epNew = null;
string mt = ici[ii].MimeType;
try
{
epNew = bmNew.GetEncoderParameterList(ici
[ii].Clsid);
if (mt == "image/jpeg")
{
bmNew.Save
("C:\\Work\\ImageConvert\\iNewJPG.jpg", ici[ii], epNew);
}
}
catch (Exception e1)
{
throw e1;
}
}

I find that out of all the four Params of epNew none of
them has the Guid of Encoder.Compression.Guid.

The GDI+ PSDK article talks about setting the compression
by changing the value of the EncoderQuality. But the
PointerValue member in C# is hidden because of protection
level. Please suggest a way other than re-writing my
entire code in C++.

Regards and Thanks.
SKD.

John Hornick [MS]

unread,
Jun 6, 2002, 1:06:36 PM6/6/02
to
Hi,

> Now I did this
>
> ImageCodecInfo[] ici = ImageCodecInfo.GetImageEncoders();
> for (int ii=0; ii<ici.Length; ii++)
> {
> EncoderParameters epNew = null;
> string mt = ici[ii].MimeType;
> try
> {
> epNew = bmNew.GetEncoderParameterList(ici
> [ii].Clsid);
> if (mt == "image/jpeg")
> {
> bmNew.Save
> ("C:\\Work\\ImageConvert\\iNewJPG.jpg", ici[ii], epNew);
> }
> }
> catch (Exception e1)
> {
> throw e1;
> }
> }
>
> I find that out of all the four Params of epNew none of
> them has the Guid of Encoder.Compression.Guid.
>
> The GDI+ PSDK article talks about setting the compression
> by changing the value of the EncoderQuality. But the
> PointerValue member in C# is hidden because of protection
> level. Please suggest a way other than re-writing my
> entire code in C++.


private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
private void SaveJPGWithCompressionSetting( Image image, string
szFileName, long lCompression )
{
EncoderParameters eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter( Encoder.Quality, lCompression );
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
image.Save( szFileName, ici, eps );

0 new messages