System.Drawing image colors are not matching web colors

266 views Asked by At

i have this simple code for image creating

Color clr = ColorTranslator.FromHtml("#0000ff");
using (var bm = new Bitmap(100, 100)) {
    using (Graphics g = Graphics.FromImage(bm))
        g.Clear(clr);
    bm.Save("e:\\blue-square.gif", ImageFormat.Gif);
    bm.Save("e:\\blue-square.jpg", ImageFormat.Jpeg);
    bm.Save("e:\\blue-square.png", ImageFormat.Png);
}

and here is an example

Why colors in png and jpg are different from html #0000ff color? How to create transparent png with colored element in it with correct color?

update 1

Bitmap.Save(ImageFormat) has default parameters and i've done some research in this direction.

I decided to start with jpg and my goal is clean #0000ff after save.

There is another save method Bitmap.Save(ImageCodecInfo,EncoderParameters) and nice example about jpeg quality https://msdn.microsoft.com/en-us/en-en/library/bb882583(v=vs.110).aspx

I've tried 100L but this is not solve my problem - the color is #0000fe instead of #0000ff

It seems i have to set some other parameters in EncoderParameters object.

I read this https://msdn.microsoft.com/en-us/en-en/library/bb882589(v=vs.110).aspx
and this Unexpected "Bitmap Region is already Locked" exception with GetEncoderParameterList. Any ideas?

jpeg still no success

update 2

I tried png and custom palette with pixel format PixelFormat.Format8bppIndexed and result was the same: #3000fe

1

There are 1 answers

6
Yury Glushkov On

It's an issue of image compression algorithms