private void CreateImg(Stream OutStream, ImageFormat ImgFormat, ImageColorDepth Colors) { TImgExportInfo ExportInfo = null; PixelFormat RgbPixFormat = Colors != ImageColorDepth.TrueColor ? PixelFormat.Format32bppPArgb : PixelFormat.Format24bppRgb; PixelFormat PixFormat = PixelFormat.Format1bppIndexed; switch (Colors) { case ImageColorDepth.TrueColor: PixFormat = RgbPixFormat; break; case ImageColorDepth.Color256: PixFormat = PixelFormat.Format8bppIndexed; break; } using (Bitmap OutImg = CreateBitmap(Resolution, ref ExportInfo, PixFormat)) { Bitmap ActualOutImg = Colors != ImageColorDepth.TrueColor? CreateBitmap(Resolution, ref ExportInfo, RgbPixFormat): OutImg; try { using (Graphics Gr = Graphics.FromImage(ActualOutImg)) { Gr.FillRectangle(Brushes.White, 0, 0, ActualOutImg.Width, ActualOutImg.Height); //Clear the background ExportNext(Gr, ref ExportInfo); } if (Colors == ImageColorDepth.BlackAndWhite) { FloydSteinbergDither.ConvertToBlackAndWhite(ActualOutImg, OutImg); } else if (Colors == ImageColorDepth.Color256) { OctreeQuantizer.ConvertTo256Colors(ActualOutImg, OutImg); } } finally { if (ActualOutImg != OutImg) { ActualOutImg.Dispose(); } } OutImg.Save(OutStream, ImgFormat); } }
private TImgExportInfo ExportAllImagesButFirst(EncoderParameters ep, bool Is1bpp, TImgExportInfo ExportInfo, PixelFormat RgbPixFormat, Bitmap OutImg, ImageColorDepth ColorDepth) { if (ExportInfo == null) { ExportInfo = GetFirstPageExportInfo(); } for (int i = ExportInfo.CurrentPage; i < ExportInfo.TotalPages; i++) { using (Bitmap TmpImg = CreateBitmap(Resolution, ref ExportInfo, RgbPixFormat)) { using (Graphics Gr = Graphics.FromImage(TmpImg)) { Gr.FillRectangle(Brushes.White, 0, 0, TmpImg.Width, TmpImg.Height); //Clear the background ExportNext(Gr, ref ExportInfo); if (Is1bpp) { using (Bitmap BwImg = FloydSteinbergDither.ConvertToBlackAndWhite(TmpImg)) { OutImg.SaveAdd(BwImg, ep); } } else if (ColorDepth == ImageColorDepth.Color256) { using (Bitmap IndexImg = OctreeQuantizer.ConvertTo256Colors(TmpImg)) { OutImg.SaveAdd(IndexImg, ep); } } else { OutImg.SaveAdd(TmpImg, ep); } } } } return(ExportInfo); }
private void CreateMultiPageTiff(Stream OutStream, ImageColorDepth ColorDepth, ImageExportType ExportType) { ImageCodecInfo info = GetTiffEncoder(); int ParamCount = 1; if (ExportType == ImageExportType.Fax || ExportType == ImageExportType.Fax4) { ParamCount++; } EncoderParameters ep = new EncoderParameters(ParamCount); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); bool IsFax = false; switch (ExportType) { case ImageExportType.Fax: ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT3); IsFax = true; break; case ImageExportType.Fax4: ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); IsFax = true; break; } bool Is1bpp = IsFax || ColorDepth == ImageColorDepth.BlackAndWhite; TImgExportInfo ExportInfo = null; PixelFormat RgbPixFormat = IsFax || ColorDepth != ImageColorDepth.TrueColor ? PixelFormat.Format32bppPArgb : PixelFormat.Format24bppRgb; PixelFormat PixFormat = PixelFormat.Format1bppIndexed; if (!IsFax) { switch (ColorDepth) { case ImageColorDepth.TrueColor: PixFormat = RgbPixFormat; break; case ImageColorDepth.Color256: PixFormat = PixelFormat.Format8bppIndexed; break; } } using (Bitmap OutImg = CreateBitmap(Resolution, ref ExportInfo, PixFormat)) { //First image is handled differently. Bitmap ActualOutImg = Is1bpp || ColorDepth != ImageColorDepth.TrueColor ? CreateBitmap(Resolution, ref ExportInfo, RgbPixFormat) : OutImg; try { using (Graphics Gr = Graphics.FromImage(ActualOutImg)) { Gr.FillRectangle(Brushes.White, 0, 0, ActualOutImg.Width, ActualOutImg.Height); //Clear the background ExportNext(Gr, ref ExportInfo); } if (Is1bpp) { FloydSteinbergDither.ConvertToBlackAndWhite(ActualOutImg, OutImg); } else if (!IsFax && ColorDepth == ImageColorDepth.Color256) { OctreeQuantizer.ConvertTo256Colors(ActualOutImg, OutImg); } } finally { if (ActualOutImg != OutImg) { ActualOutImg.Dispose(); } } OutImg.Save(OutStream, info, ep); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); //Now the rest of images. ExportInfo = ExportAllImagesButFirst(ep, Is1bpp, ExportInfo, RgbPixFormat, OutImg, ColorDepth); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); OutImg.SaveAdd(ep); } }