GDIplus issue

  • Thread starter Alvin Bruney - ASP.NET MVP
  • Start date
A

Alvin Bruney - ASP.NET MVP

I'm using C# and GDI+ to create dynamic images and show them on the
web. I'm loading the main image which is a jpeg, then I'm drawing some
text and shapes on that base image using Graphics class of GDI+, at the
end I'm sending the new image to output.

My problem is when I use GDI+, I loose some quality of the base image.
Although I'm using jpeg Compression = 100, I still have some artifacts
on the output image. Some colors on the base image are also lost.

I load the base jpeg image as a bitmap, It doesn't have any palettes
after I load it. and I can't set any palettes for it, to do some
possible changes on pallete and improve the quality.

How can I improve the quality WITHOUT using UNSAFE code in C#?

Any help appreciated.

Thanks,

here is my code:

public void CreateDynamicImage()
{
WebClient webclient = new WebClient();
bmpBook=(Bitmap)
System.Drawing.Image.FromStream(webclient.OpenRead(urlBaseImage));

EncoderParameter testParam= new
EncoderParameter(Encoder.Compression,100);
EncoderParameters encoderParams= new EncoderParameters(1);
encoderParams.Param[0]= testParam;
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

bmpFinal= new Bitmap(bmpBook.Width, bmpBook.Height);
System.Drawing.Graphics g =
System.Drawing.Graphics.FromImage(bmpFinal);
g.DrawImage(bmpBook, 0, 0, bmpBook.Width, bmpBook.Height);

//drawing some other texts and shapes
...

Response.Clear();
Response.ContentType="image/jpeg";
bmpFinal.Save(Response.OutputStream,jpegCodec,encoderParams);

bmpBook.Dispose();
bmpFinal.Dispose();
g.Dispose();
}


--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------
 
S

SnarfBlam

What you are looking for is the Quaility parameter, not the Compression
parameter. For some reason the parameter needs to be passed as
(therefore cast to) a long (I'm guessing it's an overload thing).

EncoderParameter testParam = new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
//100 is probably more than you need.
 
A

Alvin Bruney - ASP.NET MVP

Thanks, that worked like a treat.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top