public static void Save(MemBitmap bmp, Stream strm) { //------------- unsafe { PixelFarm.CpuBlit.Imaging.TempMemPtr tmp = MemBitmap.GetBufferPtr(bmp); int *intBuffer = (int *)tmp.Ptr; int imgW = bmp.Width; int imgH = bmp.Height; Hjg.Pngcs.ImageInfo imgInfo = new Hjg.Pngcs.ImageInfo(imgW, imgH, 8, true); //8 bits per channel with alpha Hjg.Pngcs.PngWriter writer = new Hjg.Pngcs.PngWriter(strm, imgInfo); Hjg.Pngcs.ImageLine iline = new Hjg.Pngcs.ImageLine(imgInfo, Hjg.Pngcs.ImageLine.ESampleType.BYTE); int startReadAt = 0; int imgStride = imgW * 4; int srcIndex = 0; int srcIndexRowHead = (tmp.LengthInBytes / 4) - imgW; for (int row = 0; row < imgH; row++) { byte[] scanlineBuffer = iline.ScanlineB; srcIndex = srcIndexRowHead; for (int b = 0; b < imgStride;) { int srcInt = intBuffer[srcIndex]; srcIndex++; scanlineBuffer[b] = (byte)((srcInt >> 16) & 0xff); scanlineBuffer[b + 1] = (byte)((srcInt >> 8) & 0xff); scanlineBuffer[b + 2] = (byte)((srcInt) & 0xff); scanlineBuffer[b + 3] = (byte)((srcInt >> 24) & 0xff); b += 4; } srcIndexRowHead -= imgW; startReadAt += imgStride; writer.WriteRow(iline, row); } writer.End(); } }
static void SaveImgBufferToFile(GlyphImage glyphImg, string filename) { //------------- int[] intBuffer = glyphImg.GetImageBuffer(); using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create)) { int imgW = glyphImg.Width; int imgH = glyphImg.Height; Hjg.Pngcs.ImageInfo imgInfo = new Hjg.Pngcs.ImageInfo(imgW, imgH, 8, true); //8 bits per channel with alpha Hjg.Pngcs.PngWriter writer = new Hjg.Pngcs.PngWriter(fs, imgInfo); Hjg.Pngcs.ImageLine iline = new Hjg.Pngcs.ImageLine(imgInfo, Hjg.Pngcs.ImageLine.ESampleType.BYTE); int startReadAt = 0; int imgStride = imgW * 4; int srcIndex = 0; int srcIndexRowHead = intBuffer.Length - imgW; for (int row = 0; row < imgH; row++) { byte[] scanlineBuffer = iline.ScanlineB; srcIndex = srcIndexRowHead; for (int b = 0; b < imgStride;) { int srcInt = intBuffer[srcIndex]; srcIndex++; scanlineBuffer[b] = (byte)((srcInt >> 16) & 0xff); scanlineBuffer[b + 1] = (byte)((srcInt >> 8) & 0xff); scanlineBuffer[b + 2] = (byte)((srcInt) & 0xff); scanlineBuffer[b + 3] = (byte)((srcInt >> 24) & 0xff); b += 4; } srcIndexRowHead -= imgW; startReadAt += imgStride; writer.WriteRow(iline, row); } writer.End(); } }