/// <summary> /// Returns a native Picture object /// </summary> /// <param name="autoDispose">Release the image resources once done</param> /// <returns></returns> Interop.IStdPicture ComInterfaces._Image.GetPicture(bool autoDispose) { Interop.IStdPicture picture = ImgExt.ImageToPictureDisp(_bitmap); if (autoDispose) { _bitmap.Dispose(); } return(picture); }
/// <summary> /// Encodes a bitmap to PNG and writes it to a stream. /// </summary> /// <param name="bitmap"></param> /// <param name="destStream"></param> /// <returns>Lengths of the encoded data</returns> public static int WritePNG(Bitmap bitmap, Stream destStream) { //encode the bitmap to PNG MemoryStream buffer = new MemoryStream(); if (bitmap.PixelFormat != PixelFormat.Format24bppRgb) { //Converts to a 3 channels RGB bitmmap as ARGB is not unsupported using (Bitmap tmpBitmap = ImgExt.RemoveAlphaChannel(bitmap)) { tmpBitmap.Save(buffer, ENCODER_PNG, null); } } else { bitmap.Save(buffer, ENCODER_PNG, null); } var bytes = buffer.GetBuffer(); //Since a PDF file doesn't support the PNG format, the encoded chunks need //to be extracted from it. //PNG chunk: [length 4 bytes BE][type 4 bytes][data of length][CRC 4bytes] int imageLength = 0; int offset = 8; //starts reading just after the 8 bytes signature int offsetBreak = (int)buffer.Position - 8; //max length - min read length in a loop while (offset < offsetBreak) { //read the length on 4 bytes BE int chunkLength = ReadInt32BE(bytes, offset); //read the type on 4 bytes int chunkType = ReadInt32BE(bytes, offset + 4); if (chunkType == 0x49444154) //if chunkType equals "IDAT" //Writes the chunk to the stream { destStream.Write(bytes, offset + 8, chunkLength); imageLength += chunkLength; } //move to the next chunk offset += chunkLength + 12; } return(imageLength); }