/// <summary> /// writes the image to the specified stream /// </summary> public override void Write(Stream str) { if (str == null) { throw new ArgumentNullException("str"); } //write header BITMAPINFOHEADER header = new BITMAPINFOHEADER( _bitmap.Size, 24, this.SizeInBytes, 0); header.Write(str); #region write 24bpp data //write bitmap BitmapData bd = _bitmap.LockBits( new Rectangle(Point.Empty, _bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); ColorBgra *scan0 = (ColorBgra *)bd.Scan0; //calculate padding at end of stride int padbytescount = (_bitmap.Width * 3) % 4; byte[] padbytes = null; if (padbytescount != 0) { padbytes = new byte[padbytescount]; } //write pixels, note: bottom-up, left-right using (SimpleWriter wrt = new SimpleWriter(str)) { scan0 += bd.Width * bd.Height; for (int y = 0; y < bd.Height; y++) { scan0 -= bd.Width; for (int x = 0; x < bd.Width; x++) { wrt.Write(scan0[x].Blue); wrt.Write(scan0[x].Green); wrt.Write(scan0[x].Red); } //pad to 32bit if (padbytes != null) { wrt.Write(padbytes); } } ANDMap.WriteToStream(bd, wrt); } _bitmap.UnlockBits(bd); #endregion }
/// <summary> /// writes the image to the specified stream /// </summary> public override void Write(Stream str) { if (str == null) { throw new ArgumentNullException("str"); } //write header BITMAPINFOHEADER header = new BITMAPINFOHEADER( _bitmap.Size, 32, this.SizeInBytes, 0); header.Write(str); #region write 32bpp data //write bitmap BitmapData bd = _bitmap.LockBits( new Rectangle(Point.Empty, _bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int *scan0 = (int *)bd.Scan0; //write pixels, note: bottom-up, left-right using (SimpleWriter wrt = new SimpleWriter(str)) { scan0 += bd.Width * bd.Height; for (int y = 0; y < bd.Height; y++) { scan0 -= bd.Width; for (int x = 0; x < bd.Width; x++) { wrt.Write(scan0[x]); } } ANDMap.WriteToStream(bd, wrt); } _bitmap.UnlockBits(bd); #endregion }
/// <summary> /// writes the image to the specified stream /// </summary> public override void Write(Stream str) { if (str == null) { throw new ArgumentNullException("str"); } //write header BITMAPINFOHEADER header = new BITMAPINFOHEADER( _bitmap.Size, _bitsperpixel, this.SizeInBytes, (1 << _bitsperpixel) & 0xFF); header.Write(str); using (SimpleWriter wrt = new SimpleWriter(str)) { #region write palette foreach (ColorBgra value in _octree.Table) { wrt.Write(value.B); wrt.Write(value.G); wrt.Write(value.R); wrt.Write((byte)0); //reserved } #endregion #region write indexed data //write bitmap BitmapData bd = _bitmap.LockBits( new Rectangle(Point.Empty, _bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); ColorBgra *scan0 = (ColorBgra *)bd.Scan0; //write indexed data int indexmask = (1 << _bitsperpixel) - 1, indexdata = 0; scan0 += bd.Width * bd.Height; for (int y = 0; y < bd.Height; y++) { scan0 -= bd.Width; int indexpos = 8 - _bitsperpixel; indexdata = 0; for (int x = 0; x < bd.Width; x++) { indexdata |= (_octree.GetOctreeIndex(scan0[x]) & indexmask) << indexpos; if (indexpos % 8 == 0) { indexpos += (16 - _bitsperpixel); } else { indexpos -= _bitsperpixel; } if (indexpos >= 32) { wrt.Write(indexdata); indexdata = 0; indexpos -= 32; } } if (indexpos != (8 - _bitsperpixel)) { wrt.Write(indexdata); } } ANDMap.WriteToStream(bd, wrt); #endregion _bitmap.UnlockBits(bd); } }