I have a handler which sets a watermark on images. The problem is the quality is not so good.
Here is the code:
byte[] imageBytes = null;
using (Graphics G = Graphics.FromImage(ImageToWatermark))
{
using (ImageAttributes IA = new ImageAttributes())
{
ColorMatrix CM = new ColorMatrix();
CM.Matrix33 = Opacity;
IA.SetColorMatrix(CM);
G.DrawImage(Watermark, new Rectangle(WatermarkPosition, Watermark.Size), 0, 0, Watermark.Width, Watermark.Height, GraphicsUnit.Pixel, IA);
}
}
using (MemoryStream memoryStream = new MemoryStream())
{
ImageToWatermark.Save(memoryStream, GetImageFormat(context.Request.PhysicalPath));
imageBytes = memoryStream.ToArray();
}
How can I set the quality for the result?
If your image format is a Jpeg, then you can set the output quality manually by using this sample method to save to disk:
Or, in your case, use the following piece of code:
In general, a quality of 60 to 100 should produce good enough Jpeg images for screen output.