public void SaveAsImage(string fileName, ImageExportType export, ImageColorDepth ColorDepth) { try { FileMode fm = FileMode.CreateNew; if (AllowOverwritingFiles) { fm = FileMode.Create; } using (FileStream f = new FileStream(fileName, fm, FileAccess.Write)) { SaveAsImage(f, export, ColorDepth); } } catch (IOException) { //Don't delete the file in an io exception. It might be because allowoverwritefiles was false, and the file existed. throw; } catch { File.Delete(fileName); throw; } }
private void DoExportUsingFlexCelImgExportSimple(ImageColorDepth ColorDepth) { if (!HasFileOpen()) { return; } if (!LoadPreferences()) { return; } if (exportImageDialog.ShowDialog() != DialogResult.OK) { return; } ImageExportType ImgFormat = ImageExportType.Png; if (String.Compare(Path.GetExtension(exportImageDialog.FileName), ".jpg", true) == 0) { ImgFormat = ImageExportType.Jpeg; } using (FlexCelImgExport ImgExport = new FlexCelImgExport(flexCelPrintDocument1.Workbook)) { ImgExport.AllVisibleSheets = cbAllSheets.Checked; ImgExport.ResetPageNumberOnEachSheet = cbResetPageNumber.Checked; ImgExport.Resolution = 96; //To get a better quality image but with larger file size too, increate this value. (for example to 300 or 600 dpi) ImgExport.SaveAsImage(exportImageDialog.FileName, ImgFormat, ColorDepth); } }
private void DoExportUsingFlexCelImgExportComplex(ImageColorDepth ColorDepth) { if (!HasFileOpen()) { return; } if (!LoadPreferences()) { return; } if (exportImageDialog.ShowDialog() != DialogResult.OK) { return; } System.Drawing.Imaging.ImageFormat ImgFormat = System.Drawing.Imaging.ImageFormat.Png; if (String.Compare(Path.GetExtension(exportImageDialog.FileName), ".jpg", true) == 0) { ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; } using (FlexCelImgExport ImgExport = new FlexCelImgExport(flexCelPrintDocument1.Workbook)) { ImgExport.Resolution = 96; //To get a better quality image but with larger file size too, increate this value. (for example to 300 or 600 dpi) if (cbAllSheets.Checked) { int SaveActiveSheet = ImgExport.Workbook.ActiveSheet; try { ImgExport.Workbook.ActiveSheet = 1; bool Finished = false; while (!Finished) { ExportAllImages(ImgExport, ImgFormat, ColorDepth); if (ImgExport.Workbook.ActiveSheet < ImgExport.Workbook.SheetCount) { ImgExport.Workbook.ActiveSheet++; } else { Finished = true; } } } finally { ImgExport.Workbook.ActiveSheet = SaveActiveSheet; } } else { ExportAllImages(ImgExport, ImgFormat, ColorDepth); } } }
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); } }
//How to create a multipage tiff using FlexCelImgExport. //This will create a multipage tiff with the data. private void DoExportMultiPageTiff(ImageColorDepth ColorDepth, bool IsFax) { if (!HasFileOpen()) { return; } if (!LoadPreferences()) { return; } if (exportTiffDialog.ShowDialog() != DialogResult.OK) { return; } ImageExportType ExportType = ImageExportType.Tiff; if (IsFax) { ExportType = ImageExportType.Fax; } using (FlexCelImgExport ImgExport = new FlexCelImgExport(flexCelPrintDocument1.Workbook)) { ImgExport.AllVisibleSheets = cbAllSheets.Checked; ImgExport.ResetPageNumberOnEachSheet = cbResetPageNumber.Checked; ImgExport.Resolution = 96; //To get a better quality image but with larger file size too, increate this value. (for example to 300 or 600 dpi) using (FileStream TiffStream = new FileStream(exportTiffDialog.FileName, FileMode.Create)) { ImgExport.SaveAsImage(TiffStream, ExportType, ColorDepth); } } if (MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) { Process.Start(exportTiffDialog.FileName); } }
public void SaveAsImage(Stream fileStream, ImageExportType export, ImageColorDepth ColorDepth) { if (Workbook == null) { FlxMessages.ThrowException(FlxErr.ErrWorkbookNull); } Workbook.Recalc(false); switch (export) { case ImageExportType.Tiff: CreateMultiPageTiff(fileStream, ColorDepth, export); break; case ImageExportType.Fax: CreateMultiPageTiff(fileStream, ColorDepth, export); break; case ImageExportType.Fax4: CreateMultiPageTiff(fileStream, ColorDepth, export); break; case ImageExportType.Gif: CreateImg(fileStream, ImageFormat.Gif, ImageColorDepth.Color256); break; case ImageExportType.Png: CreateImg(fileStream, ImageFormat.Png, ColorDepth); break; case ImageExportType.Jpeg: CreateImg(fileStream, ImageFormat.Jpeg, ColorDepth); break; default: FlxMessages.ThrowException(FlxErr.ErrInvalidImageFormat); break; } }
private void ExportAllImages(FlexCelImgExport ImgExport, ImageFormat ImgFormat, ImageColorDepth ColorDepth) { TImgExportInfo ExportInfo = null; //For first page. int i = 0; do { string FileName = Path.GetDirectoryName(exportImageDialog.FileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(exportImageDialog.FileName) + "_" + ImgExport.Workbook.SheetName + String.Format("_{0:0000}", i) + Path.GetExtension(exportImageDialog.FileName); using (FileStream ImageStream = new FileStream(FileName, FileMode.Create)) { CreateImg(ImageStream, ImgExport, ImgFormat, ColorDepth, ref ExportInfo); } i++; } while (ExportInfo.CurrentPage < ExportInfo.TotalPages); }
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); } }
/// <summary> /// Save document as Tiff image. /// </summary> /// <param name="name">The document name.</param> /// <param name="resultFile">The resulting file. Resulting image saved to the same folder and storage where the original document is. Relative path can be used here for some subfolder of the document folder.</param> /// <param name="brightness">The image brightness.</param> /// <param name="compression">Tiff compression. Possible values are: LZW, CCITT4, CCITT3, RLE, None.</param> /// <param name="colorDepth">Image color depth. Possible valuse are: Default, Format8bpp, Format4bpp, Format1bpp.</param> /// <param name="leftMargin">Left image margin.</param> /// <param name="rightMargin">Right image margin.</param> /// <param name="topMargin">Top image margin.</param> /// <param name="bottomMargin">Bottom image margin.</param> /// <param name="orientation">Image orientation. Possible values are: None, Landscape, Portait.</param> /// <param name="skipBlankPages">Skip blank pages flag.</param> /// <param name="width">Image width.</param> /// <param name="height">Image height.</param> /// <param name="xResolution">Horizontal resolution.</param> /// <param name="yResolution">Vertical resolution.</param> /// <param name="pageIndex">Start page to export.</param> /// <param name="pageCount">Number of pages to export.</param> /// <param name="storage">The document storage.</param> /// <param name="folder">The document folder.</param> public void SaveDocumentAsTiffImage(string name, string resultFile, string folder, double brightness = 0.0, TiffCompression compression = TiffCompression.None, ImageColorDepth colorDepth = ImageColorDepth.Default, int leftMargin = 0, int rightMargin = 0, int topMargin = 0, int bottomMargin = 0, Orientation orientation = Orientation.None, bool skipBlankPages = true, int width = 0, int height = 0, int xResolution = 0, int yResolution = 0, int pageIndex = 0, int pageCount = 0, string storage = "") { // PUT pdf/{name}/SaveAs/tiff?appSid={appSid}&resultFile={resultFile}&brightness={brightness}&compression={compression}&colorDepth={colorDepth}&leftMargin={leftMargin}&rightMargin={rightMargin}&topMargin={topMargin}&bottomMargin={bottomMargin}&orientation={orientation}&skipBlankPages={skipBlankPages}&width={width}&height={height}&xResolution={xResolution}&yResolution={yResolution}&pageIndex={pageIndex}&pageCount={pageCount}&storage={storage}&folder={folder} string apiUrl = string.Format(@"pdf/{0}/SaveAs/tiff?resultFile={1}&skipBlankPages={2}&storage={3}&folder={4}", name, resultFile, skipBlankPages, storage, folder); if (brightness > 0) apiUrl += string.Format("&brightness={0}", brightness); if (compression != TiffCompression.None) apiUrl += string.Format("&compression={0}", compression); if (colorDepth > 0) apiUrl += string.Format("&colorDepth={0}", colorDepth); if (leftMargin > 0) apiUrl += string.Format("&leftMargin={0}", leftMargin); if (rightMargin > 0) apiUrl += string.Format("&rightMargin={0}", rightMargin); if (topMargin > 0) apiUrl += string.Format("&topMargin={0}", topMargin); if (bottomMargin > 0) apiUrl += string.Format("&bottomMargin={0}", bottomMargin); if (orientation != Orientation.None) apiUrl += string.Format("&orientation={0}", orientation); if (width > 0) apiUrl += string.Format("&width={0}", width); if (height > 0) apiUrl += string.Format("&height={0}", height); if (rightMargin > 0) apiUrl += string.Format("&rightMargin={0}", rightMargin); if (yResolution > 0) apiUrl += string.Format("&yResolution={0}", yResolution); if (pageIndex > 0) apiUrl += string.Format("&pageIndex={0}", pageIndex); if (pageCount > 0) apiUrl += string.Format("&pageCount={0}", pageCount); ServiceController.Put(apiUrl, AppSid, AppKey); }