Working with different image format
-
In my application, I want to read images which are in the PNG format, 256 colors, with transparency, store them in memory and then display them on screen. So I do something like that : Bitmap[,] myBitmapArray; ... myBitmapArray[i,j] = new Bitmap ("FileName.png"); ... graphics.DrawImage(myBitmapArray[i,j]); The pb is my application takes 100 Mb in memory to load the pictures, which are only 4 Mb in the files :mad: Apparently, when loading a picture, there is no format specifier. So I thought it should use the file, but when looking at the PixelFormat member, it's always 32bpp... So I have tried to create the bitmap another way, like new Bitmap (128,64, System.Drawing.Imaging.PixelFormat.PixelFormat8bppIndexed), and then copy my image from the file here. But : - If I use 16bpp555, I lose the transparency - If I use 16bpp1555, JIT debugging tells me "out of memory" - If I use 8bppIndexed, JIT debugging tells me I cannot create a graphics (graphics.FromImage -> error). How can I load my 256 color files and draw them in a picture box, without taking so much memory because they are transformed in true color?
-
In my application, I want to read images which are in the PNG format, 256 colors, with transparency, store them in memory and then display them on screen. So I do something like that : Bitmap[,] myBitmapArray; ... myBitmapArray[i,j] = new Bitmap ("FileName.png"); ... graphics.DrawImage(myBitmapArray[i,j]); The pb is my application takes 100 Mb in memory to load the pictures, which are only 4 Mb in the files :mad: Apparently, when loading a picture, there is no format specifier. So I thought it should use the file, but when looking at the PixelFormat member, it's always 32bpp... So I have tried to create the bitmap another way, like new Bitmap (128,64, System.Drawing.Imaging.PixelFormat.PixelFormat8bppIndexed), and then copy my image from the file here. But : - If I use 16bpp555, I lose the transparency - If I use 16bpp1555, JIT debugging tells me "out of memory" - If I use 8bppIndexed, JIT debugging tells me I cannot create a graphics (graphics.FromImage -> error). How can I load my 256 color files and draw them in a picture box, without taking so much memory because they are transformed in true color?
i think you mean Format16bppArgb1555? you could hack it an store the byte[] data from the image in an array, and then only create the bitmap when you need it..
"When the only tool you have is a hammer, a sore thumb you will have."
-
In my application, I want to read images which are in the PNG format, 256 colors, with transparency, store them in memory and then display them on screen. So I do something like that : Bitmap[,] myBitmapArray; ... myBitmapArray[i,j] = new Bitmap ("FileName.png"); ... graphics.DrawImage(myBitmapArray[i,j]); The pb is my application takes 100 Mb in memory to load the pictures, which are only 4 Mb in the files :mad: Apparently, when loading a picture, there is no format specifier. So I thought it should use the file, but when looking at the PixelFormat member, it's always 32bpp... So I have tried to create the bitmap another way, like new Bitmap (128,64, System.Drawing.Imaging.PixelFormat.PixelFormat8bppIndexed), and then copy my image from the file here. But : - If I use 16bpp555, I lose the transparency - If I use 16bpp1555, JIT debugging tells me "out of memory" - If I use 8bppIndexed, JIT debugging tells me I cannot create a graphics (graphics.FromImage -> error). How can I load my 256 color files and draw them in a picture box, without taking so much memory because they are transformed in true color?
I've made some further testing, and discovered it comes from the format. With two similar picture, 256 colors,one is png the other is gif Bitmap bitmap = new Bitmap("Myfile.png"); -> bitmap.PixelFormat = PixelFormat.Format32bppArgb Bitmap bitmap = new Bitmap("Myfile.gif"); -> bitmap.PixelFormat = PixelFormat.Format8bppIndexed Conclusion : The png codec is "buggy" and if you use the standard constructor, the bitmap ignore the file attributes and is always 32bppp. The gif coded works OK, and the bitmap keeps the file attributes. With this, I've reduced my application memory usage by 60%