load a lot of Images to memory
-
The OP stated "every bitmap in the memory takes about 10 times more then in the file" so I assume compression has been doing a fine job on his original images and stands a good chance of doing the same on the final images. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Fair enough, but, who knows right? PNGOUT-ed PNGs can be a lot smaller than PNGs compressed by the .NET PNG compressor..
given time the OP will tell. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
hi i want to load a lot of images to the memory. of course every bitmap in the memory takes about 10 times more then in the file; so (if there is no other way) i'm loading the image as a byte array: private byte[] FileToByteArray (string path) { byte[] bytes = null; using (System.IO.Stream stream = File.OpenRead(path)) { using (System.IO.BinaryReader reader = new BinaryReader(stream)) { bytes = reader.ReadBytes((int)reader.BaseStream.Length); } } return bytes; } and store it into a byte array variable. when i need the image i use: private Image ByteArrayToImage(byte[] byteArrayIn) { if (byteArrayIn == null) return null; MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; } so far so good, the problem is if the image need to be change i send it to a function and get new bitmap, now i want to save it back to byte array, so if i use: private byte[] ImageToByteArray(System.Drawing.Image imageIn) { if (imageIn == null) return null; MemoryStream ms = new MemoryStream(); imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Png); imageIn.Dispose(); return ms.ToArray(); } i get a byte array that is about 10 times bigger. so how can i get a smaller byte array?
-
hi i want to load a lot of images to the memory. of course every bitmap in the memory takes about 10 times more then in the file; so (if there is no other way) i'm loading the image as a byte array: private byte[] FileToByteArray (string path) { byte[] bytes = null; using (System.IO.Stream stream = File.OpenRead(path)) { using (System.IO.BinaryReader reader = new BinaryReader(stream)) { bytes = reader.ReadBytes((int)reader.BaseStream.Length); } } return bytes; } and store it into a byte array variable. when i need the image i use: private Image ByteArrayToImage(byte[] byteArrayIn) { if (byteArrayIn == null) return null; MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; } so far so good, the problem is if the image need to be change i send it to a function and get new bitmap, now i want to save it back to byte array, so if i use: private byte[] ImageToByteArray(System.Drawing.Image imageIn) { if (imageIn == null) return null; MemoryStream ms = new MemoryStream(); imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Png); imageIn.Dispose(); return ms.ToArray(); } i get a byte array that is about 10 times bigger. so how can i get a smaller byte array?
nryk wrote:
private byte[] FileToByteArray (string path)
Just use the
File.ReadAllBytes
method.nryk wrote:
of course every bitmap in the memory takes about 10 times more then in the file
nryk wrote:
i get a byte array that is about 10 times bigger
From that I can deduct that you either managed to change the image into an image that is ten times larger (e.g. three times larger in both x and y axis), or you have changed it into something that the Save method could not compress at all. Could you post example image files somewhere for examination?
Despite everything, the person most likely to be fooling you next is yourself.
-
nryk wrote:
private byte[] FileToByteArray (string path)
Just use the
File.ReadAllBytes
method.nryk wrote:
of course every bitmap in the memory takes about 10 times more then in the file
nryk wrote:
i get a byte array that is about 10 times bigger
From that I can deduct that you either managed to change the image into an image that is ten times larger (e.g. three times larger in both x and y axis), or you have changed it into something that the Save method could not compress at all. Could you post example image files somewhere for examination?
Despite everything, the person most likely to be fooling you next is yourself.
i managed to see the problem: i'm loading jpg files, but in my app i need it as png file ( because of the alpha channel )so when i save it to the memoryStreem as ImageFormat.Png the png is less compressed,is it possible to compress the png file to something like 0.5 mb - 1 mb like jpg ?
-
i need the png format because i'm useing the alpha channel. how does other programs do that ? for example powerPoint ? in powerPoint i can put tens of images and the memory grows only with a few mb ?
Once you create the Bitmap object, in memory, the image is no longer compressed, but is a full 32-bit render of what the file says it should look like.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
i managed to see the problem: i'm loading jpg files, but in my app i need it as png file ( because of the alpha channel )so when i save it to the memoryStreem as ImageFormat.Png the png is less compressed,is it possible to compress the png file to something like 0.5 mb - 1 mb like jpg ?
That's hard when the source is a jpg. The PNG will preserve all the JPG compression artifacts resulting in a much bigger file (compared to what it would have been if the original source had directly been PNG-compressed) which essentially doesn't contain any extra useful information - except possibly for the Alpha channel (if present).
-
i managed to see the problem: i'm loading jpg files, but in my app i need it as png file ( because of the alpha channel )so when i save it to the memoryStreem as ImageFormat.Png the png is less compressed,is it possible to compress the png file to something like 0.5 mb - 1 mb like jpg ?
No, there is no lossy compression for PNG. The high compression rate of JPEG is simply done by throwing away the data that has least visual impact on the result. It would of course be possible to write your own compression algorithm that does that, but it would be very hard to get the same quality - compression ratio as JPEG compression, as the compression used in a PNG is not at all designed for lossy compression. Another alternative that is a bit more promising would be to extract the color data and alpha channel separately, and compress the color data as a regular JPEG image and the alpha data as a grayscale JPEG image.
Despite everything, the person most likely to be fooling you next is yourself.
-
No, there is no lossy compression for PNG. The high compression rate of JPEG is simply done by throwing away the data that has least visual impact on the result. It would of course be possible to write your own compression algorithm that does that, but it would be very hard to get the same quality - compression ratio as JPEG compression, as the compression used in a PNG is not at all designed for lossy compression. Another alternative that is a bit more promising would be to extract the color data and alpha channel separately, and compress the color data as a regular JPEG image and the alpha data as a grayscale JPEG image.
Despite everything, the person most likely to be fooling you next is yourself.
Guffa wrote:
Another alternative that is a bit more promising would be to extract the color data and alpha channel separately, and compress the color data as a regular JPEG image and the alpha data as a grayscale JPEG image.
and how do i do that ? and how do i combine them together later ? is it with LockBits? and run through every "Pixel" ? is there any other file type like png that have the alpha channel and still remain small size ?
-
Guffa wrote:
Another alternative that is a bit more promising would be to extract the color data and alpha channel separately, and compress the color data as a regular JPEG image and the alpha data as a grayscale JPEG image.
and how do i do that ? and how do i combine them together later ? is it with LockBits? and run through every "Pixel" ? is there any other file type like png that have the alpha channel and still remain small size ?
Yes, you would use LockBits to get access to the pixel data. No, there isn't any image format with alpha channel and the compression ratio of JPEG, at least not supported by the .NET framework.
Despite everything, the person most likely to be fooling you next is yourself.