c# - Why do I get completely different results when saving a BitmapSource to bmp, jpeg, and png in WPF -


I've written a little utility class which protects BitmapSource objects from image files. Image files can be either BMP, JPG, or PNG, here's the code:

  public square bitmapprosessor {public Zero SAVSBMP (bitmapsource bitmapsource, string path) {save (bitmap source, path, new BMPTemap Encoder ()); } Public Zero SaveAsJpg (bitmapsource bitmap source, string path) {save (bitmap source, path, new jpegbatmap encoder); } Public Zero SaveAsPng (bitmapsource bitmap source, string path) {save (bitmap source, path, new PngBitmapEncoder ()); } Private Zero Save (bitmap source bitmap source, string path, bitmap encoder encoder) {(var stream = new filestream (path, filemod. Creat)) {encoder.Frames.Add (bitmapfree.create (bitmap source)); Encoder.Save (stream); }}}  

Each of the three save works, but I get unexpected results with BMP and JPEG. I PGG is the only format What I am seeing, if I show the BitmapSource on the screen using WPF Image control then produces exact reproduction.

here

> jpeg - very saturated


PNG - correct


Why am I getting completely different results for different file types?

I should keep in mind that my example in BitmapSource uses alpha values ​​of 0.1 (this is why it appears very desaturated), but any image format Should be possible to show the resulting colors in I know that I capture a screen using something like a hyper snap, it will be true that what type of file type I save.


Here the hyperspace screen capture is saved as BMP:

As you can see, this is not a problem, so Certainly there's something weird about WPF's image encoders.

Do I have a setting wrong? Am I missing something?

I personally do not see what you are seeing. BMP and JPG do not support ambiguity and PNG

Take this code, which makes partially transparent blue rectangle in an image.

  writeable bitmap BM = new writable bitmap (100, 100, 96, 96, pixel format.pbgra32, zero); Bm.Lock (); Bitmap BMP = New Bitmap (BM PixelWith, BM PixelHight, BM BackfirstStrad, System. Drawing Imaging Pixel Format.format 32bpARBB, BM Backbuffer); Use (graphics g = graphics. Framesize (bmp)) {var color = System.Drawing.Color.FromArgb (20, System.Drawing.Color.Blue); G.FillRectangle (New system drawing. Solid brush (color), new rectangle (0, 0, BMP wide, BMP.)); } Bmp.Save (@ ". \ 000_foo.bmp", System.Drawing.Imaging.ImageFormat.Bmp); Bmp.Save (@ ". \ 000_foo.jpg", System.Drawing.imaging.mageformat.jpg); Bmp.Save (@ ". \ 000_foo.png", System.Drawings.imaging.imageformat.png); Bmp.Dispose (); Bm.AddDirtyRect (new Int32Rect (0, 0, bm.PixelWidth, bm.PixelHeight)); Bm.Unlock (); New Bitmap Processor () SaveAsBmp (BM, @ ". \ Foo.bmp"); New Bitmap Processor (.) SaveAsJpg (BM, @ ". \ Foo.jpg"); New Bitmap Processor () SaveAsPng (BM, @ ". \ Foo.png");  

PNG format always works, even if it is a system. Dressing or WPF encoders. JPG and BMP encoders do not work, they show a solid blue rectangle.

The key here is that I failed to specify a background color in my image. Without background color, the image formats will not render correctly, which do not support alpha channels (BMP / JPG). With an extra line of code:

  Clean (system, drawing collar. White); G.FillRectangle (New system drawing. Solid brush (color), new rectangle (0, 0, BMP wide, BMP.));  

My image has a background color, so encoders that do not support alpha channels can determine that the output color should be per pixel. Now all of my images look correct.

In your case, you should specify that you should control an administrator with a background color, or paint the background color when you provide your image.

And the reason for working on your third party's print screen is FYI, because ultimately the transparent color is a background color (there is a background in the background). But inside WPF, you are working with elements that do not have a set; Using an RTB on an element does not inherit the properties of its various basic elements such as the background color.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -