static public bool StreamGetImage(BinaryReader stream, int bytes, ref ImageBPPs BPP, ref Bitmap image) { if (bytes < 4) { stream.ReadBytes(bytes); return(false); } // BPP BPP = ImageBPPs.b32argb; PixelFormat pFormat = (PixelFormat)stream.ReadInt32(); if (pFormat == PixelFormat.Format24bppRgb) { BPP = ImageBPPs.b24rgb; } if (pFormat == PixelFormat.Format16bppArgb1555) { BPP = ImageBPPs.b16a1r5g5b5; } if (pFormat == PixelFormat.Format16bppRgb565) { BPP = ImageBPPs.b16r5g6b5; } if (pFormat == PixelFormat.Format8bppIndexed) { BPP = ImageBPPs.b8; } if (pFormat == PixelFormat.Format4bppIndexed) { BPP = ImageBPPs.b4; } // Read data Bitmap bmap; using (MemoryStream ms = new MemoryStream()) { ms.Write(stream.ReadBytes(bytes - 4), 0, bytes - 4); bmap = new Bitmap(ms); } // Return image = new Bitmap(bmap.Width, bmap.Height, PixelFormat.Format32bppArgb); Graphics.FromImage(image).DrawImageUnscaled(bmap, 0, 0); return(true); }
static public void StreamPutImage(BinaryWriter stream, Bitmap image, ImageBPPs BPP) { // BPP var pFormat = PixelFormat.Format32bppArgb; if (BPP == ImageBPPs.b24rgb) { pFormat = PixelFormat.Format24bppRgb; } if (BPP == ImageBPPs.b16a1r5g5b5) { pFormat = PixelFormat.Format16bppArgb1555; } if (BPP == ImageBPPs.b16r5g6b5) { pFormat = PixelFormat.Format16bppRgb565; } if (BPP == ImageBPPs.b8) { pFormat = PixelFormat.Format8bppIndexed; } if (BPP == ImageBPPs.b4) { pFormat = PixelFormat.Format4bppIndexed; } // Prepare var bmap = new Bitmap(image.Width, image.Height, pFormat); var graphics = Graphics.FromImage(bmap); graphics.DrawImageUnscaled(image, 0, 0); // Get data using (MemoryStream ms = new MemoryStream()) { bmap.Save(ms, ImageFormat.Bmp); // Write stream.Write((int)ms.Length + 4); stream.Write((int)pFormat); stream.Write(ms.ToArray()); } }