Convert WBMP image

Asked

Viewed 26 times

0

Good afternoon,

How to convert a wbmp image to png or jpg using Asp.net core ? I tried to use the ImageSharp, but it doesn’t seem to support wbmp.

  • 1

    Present the code

  • var image = Image.Load(Pathfile); Notsupportedexception: Image cannot be Loaded. Available decoders: -BMP : Bmpdecoder -GIF : Gifdecoder -JPEG : Jpegdecoder -PNG : Pngdecoder

  • Edit your question

  • Also inform which library and version you are using

  • Leandro, as I mentioned above, I tried to use Imagesharp, but it does not support WBMP, do you have any idea how to convert WBMP to PNG ? i don’t have code that works because I haven’t found anything that supports WBMP. Do you know any libraries that support ? can help me with that ?

  • You will need to read the bytes, turn into a BMP pixel by pixel and then will be able to convert to png.

Show 1 more comment

1 answer

0


I was able to solve using the library "System.Drawing.Common" and with the code below, using as reference the post https://stackoverflow.com/questions/11927391/how-to-convert-wbmp-to-png

public static bool converter(string fileIn)
        {
            if (!File.Exists(fileIn))
            {
                return false;
            }
            byte[] data = File.ReadAllBytes(fileIn);
            int width = 0;
            int height = 0;
            int i = 2;
            for (; data[i] >> 7 == 1; i++)
            {
                width = (width << 7) | (data[i] & 0x7F);
            }
            width = (width << 7) | (data[i++] & 0x7F);
            for (; data[i] >> 7 == 1; i++)
            {
                height = (height << 7) | (data[i] & 0x7F);
            }
            height = (height << 7) | (data[i++] & 0x7F);
            int firstPixel = i;
            Bitmap png = new Bitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    png.SetPixel(x, y, (((data[firstPixel + (x / 8) + (y * ((width - 1) / 8 + 1))] >> (7 - (x % 8))) & 1) == 1) ? Color.White : Color.Black);
                }
            }
            png.Save(Path.ChangeExtension(fileIn, "png"));

            return true;
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.