fschultz / NetImageLibrary

Library to help with image handling such as resizing, cropping, applying filters (brightess, contrast, unsharpen mask, chroma key etc), watermarking, thumbnail creation, opening/saving files or streams and more.
http://kaliko.com/image-library/
Other
90 stars 27 forks source link

Create a jpeg image from transparent png gives black background #1

Closed mrcode86 closed 8 years ago

mrcode86 commented 9 years ago

When i try to create a jpg from a png stream, tha background of the jpg image is black. I try this:

        using (var Temp = System.Drawing.Image.FromStream(str))
        {
            using (var Image = new KalikoImage(Temp))
            {
                Image.BackgroundColor = Color.White;

                using (var DoneStr = new MemoryStream())
                {
                    Image.SaveJpg(DoneStr, 90);
                    DoneStr.Position = 0;

                   // Save stream
                }
            }
        }

What is wrong?

fschultz commented 9 years ago

The background color is not automatically set for transparent images, but you could achieve this by first creating an image with the right color and then paste your png over it (using the BlitImage function). The code would look like this:

    using (var Temp = System.Drawing.Image.FromStream(str))
    {
        using (var Image = new KalikoImage(Temp.Width, Temp.Height, Color.White))
        {
            Image.BlitImage(Temp);

            using (var DoneStr = new MemoryStream())
            {
                Image.SaveJpg(DoneStr, 90);
                DoneStr.Position = 0;

               // Save stream
            }
        }
    }
mrcode86 commented 9 years ago

Perfect. Thanx for your help and for a nice library. =)

mrcode86 commented 9 years ago

I found a problem with this code. When i save a jpg image, i get this: https://img.myclosetroom.com/upload/user1/original/az_635633287903244562.jpg

and when i use using (var Image = new KalikoImage(str)) { using (var DoneStr = new MemoryStream()) { Image.SavePng(DoneStr); DoneStr.Position = 0; } } i get this: https://img.myclosetroom.com/upload/user1/original/az_635633287903244562.png and thats correct. So whats wrong with the jpg image?

fschultz commented 9 years ago

It seems to have been an issue with image resolution (pixels per inch) when loading images through streams. I've fixed the issue in this branch: https://github.com/fschultz/NetImageLibrary/tree/image-load-fix

Strangely I got the same error for both the jpg and png output. But I hope this will solve the problem you are seeing as well.