/// <summary> /// Get skew angle /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="exludeBorders">Excluding borders from reaserch</param> /// <param name="percentageOfExcludableBorders">Percent of excluded borders</param> /// <returns></returns> public static float GetSkewAngle(Bitmap bitmap, bool exludeBorders = false, int percentageOfExcludableBorders = 0) { GdPictureImaging image = new GdPictureImaging(); float angle = 0; try { int imageId = image.CreateGdPictureImageFromBitmap(bitmap); if (imageId > 0) { if (exludeBorders) { int width = image.GetWidth(imageId); int height = image.GetHeight(imageId); int left = width / 100 * percentageOfExcludableBorders; int top = width / 100 * percentageOfExcludableBorders; image.Crop(imageId, left, top, width - 2 * left, height - 2 * top); } angle = image.GetSkewAngle(imageId, MaxAngleOfDeskewResearch, AngleResearchStep, true); image.ReleaseGdPictureImage(imageId); } } catch (Exception e) { } return(angle); }
/// <summary> /// Rotates image /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="angle">Angle of rotation</param> /// <param name="fillColor">Fill color</param> /// <returns></returns> public static Bitmap Rotate(Bitmap bitmap, float angle, Color fillColor) { Bitmap resultImage = bitmap; if (angle != 0) { GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); try { if (imageId > 0) { image.RotateAngleBackColor(imageId, angle, fillColor); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); } } finally { image.ReleaseGdPictureImage(imageId); } } return(resultImage); }
public ATAPY.Common.IO.Files CreatePageFiles(ATAPY.Common.IO.File PdfFile) { Trace.Assert(PdfFile != null); ATAPY.Common.IO.Files Ret = new ATAPY.Common.IO.Files(); try { using (GdPicturePDF SourcePDF = new GdPicturePDF()) { if (SourcePDF.LoadFromFile(PdfFile.FullPath, true) != GdPictureStatus.OK) { throw new Warning("Cannot open source file.\r\nSource file -" + PdfFile.FullPath); } if (SourcePDF.IsEncrypted()) { throw new Warning("Source PDF file is encrypted."); } int PagesCount = SourcePDF.GetPageCount(); using (GdPictureImaging Imaging = new GdPictureImaging()) { for (int i = 1; i <= PagesCount; i++) { SourcePDF.SelectPage(i); int RenderedPageId = SourcePDF.RenderPageToGdPictureImageEx((float)DEFAULT_RENDER_DPI, true, System.Drawing.Imaging.PixelFormat.Format24bppRgb); ATAPY.Common.IO.File RetFile = new ATAPY.Common.IO.File(PageImagesFolder, PdfFile.NameWithoutExtension + "_" + i.ToString("D3") + ".png"); Imaging.SaveAsPNG(RenderedPageId, RetFile.FullPath); Imaging.ReleaseGdPictureImage(RenderedPageId); Ret.Add(RetFile); } } SourcePDF.CloseDocument(); } } catch (Exception ex) { GlobalObjects.Log.WriteError(ex); throw; } return(Ret); }
/// <summary> /// ReadPageInfo reads the page information required for the conversion of the annotations. /// </summary> /// <param name="gdPictureImaging">The GdPictureImaging.</param> /// <param name="imageId">The image identifier.</param> /// <param name="pageCount">The number of pages.</param> /// <returns>An array with the information.</returns> private static PageInfo[] ReadPageInfo(GdPictureImaging gdPictureImaging, int imageId, int pageCount) { PageInfo[] pageInfo = new PageInfo[pageCount]; for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { // Page selection is 1 based index gdPictureImaging.SelectPage(imageId, pageIndex + 1); pageInfo[pageIndex].WidthPixels = gdPictureImaging.GetWidth(imageId); pageInfo[pageIndex].HeightPixels = gdPictureImaging.GetHeight(imageId); pageInfo[pageIndex].WidthInches = gdPictureImaging.GetWidthInches(imageId); pageInfo[pageIndex].HeightInches = gdPictureImaging.GetHeightInches(imageId); pageInfo[pageIndex].WangTagData = null; int tagCount = gdPictureImaging.TagCount(imageId); for (int tagIndex = 1; tagIndex <= tagCount; tagIndex++) { Tags tag = gdPictureImaging.TagGetID(imageId, tagIndex); if (tag == Tags.TagWangAnnotations) { int length = gdPictureImaging.TagGetLength(imageId, tagIndex); pageInfo[pageIndex].WangTagData = new byte[length]; gdPictureImaging.TagGetValueBytes(imageId, tagIndex, ref pageInfo[pageIndex].WangTagData); } } } return(pageInfo); }
private bool[] GetAreaLines(int AreaId, GdPictureImaging Imaging, int Width, int Height, out bool[] VerticalLines) { int[] Data = new int[Width * Height]; Imaging.GetPixelArrayInteger(AreaId, ref Data, 0, 0, Width, Height); bool[] Ret = new bool[Height]; VerticalLines = new bool[Width]; for (int i = 0; i < Height; i++) { Ret[i] = true; for (int j = 0; j < Width; j++) { if (Data[i * Width + j] != -1) { Ret[i] = false; break; } } } for (int i = 0; i < Width; i++) { VerticalLines[i] = true; for (int j = 0; j < Height; j++) { if (Data[i + j * Width] != -1) { VerticalLines[i] = false; break; } } } return(Ret); }
/// <summary> /// Removes blobs from image /// </summary> /// <param name="bitmap">Bitmap.</param> /// <param name="blobsDimensions">Blobs dimensions</param> /// <returns></returns> public static Bitmap RemoveBlobs(Bitmap bitmap, BlobsDimensions blobsDimensions) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); try { if (imageId > 0) { if (image.GetBitDepth(imageId) != 1) { image.ConvertTo1BppAT(imageId); } image.RemoveBlob(imageId, blobsDimensions.MinBlobWidth, blobsDimensions.MinBlobHeight, blobsDimensions.MaxBlobWidth, blobsDimensions.MaxBlobHeight, BlobRemoveMode.FavorQuality); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); } } finally { image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Removes staple marks /// </summary> /// <param name="bitmap">Bitmap</param> /// <returns></returns> public static Bitmap RemoveStapleMarks(Bitmap bitmap) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); try { if (imageId > 0) { if (image.GetBitDepth(imageId) != 1) { image.ConvertTo1BppAT(imageId); } image.RemoveStapleMark(imageId); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); } } catch { } finally { image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Converts to jpeg. /// </summary> /// <param name="outputPath">The output path.</param> public void ConvertToJpeg(string outputPath) { GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromFile(ImagePath); image.SaveAsJPEG(imageId, outputPath); image.ReleaseGdPictureImage(imageId); GC.Collect(); }
public static string castTopdf(string file, GdPictureImaging oGdPictureImaging, GdPicturePDF oGdPicturePDF) { string str = ""; oGdPictureImaging.TiffOpenMultiPageForWrite(false); int num = oGdPictureImaging.CreateGdPictureImageFromFile(file); if (num != 0) { oGdPicturePDF.NewPDF(); if (oGdPictureImaging.TiffIsMultiPage(num)) { int num1 = oGdPictureImaging.TiffGetPageCount(num); bool flag = true; int num2 = 1; while (num2 <= num1) { oGdPictureImaging.TiffSelectPage(num, num2); oGdPicturePDF.AddImageFromGdPictureImage(num, false, true); if (oGdPicturePDF.GetStat() == 0) { num2++; } else { flag = false; break; } } if (flag) { str = file.Replace(Path.GetExtension(file), ".pdf"); oGdPicturePDF.SaveToFile(file.Replace(Path.GetExtension(file), ".pdf")); if (oGdPicturePDF.GetStat() != 0) { } } oGdPicturePDF.CloseDocument(); oGdPictureImaging.ReleaseGdPictureImage(num); } else { oGdPicturePDF.AddImageFromGdPictureImage(num, false, true); if (oGdPicturePDF.GetStat() == 0) { str = file.Replace(Path.GetExtension(file), ".pdf"); if (oGdPicturePDF.SaveToFile(file.Replace(Path.GetExtension(file), ".pdf")) != 0) { } } oGdPicturePDF.CloseDocument(); oGdPictureImaging.ReleaseGdPictureImage(num); } } File.Delete(file); return(str); }
private void CheckTextWithRecognition(Page Page, int PageImageID) { using (GdPictureImaging Imaging = new GdPictureImaging()) { Imaging.OCRTesseractSetOCRContext(OCRContext.OCRContextSingleBlock); Imaging.OCRTesseractSetPassCount(2); for (int i = 0; i < Page.TextAreas.Count; i++) { var Area = Page.TextAreas[i]; if (!IsEven(Area.Text.Length)) { continue; } var Left = Area.Text.Substring(0, Area.Text.Length / 2); var Right = Area.Text.Substring(Area.Text.Length / 2); if (Left != Right) { continue; } int Width = (int)Area.Width + 2; int Height = (int)Area.Height + 2; int TempId = Imaging.CreateNewGdPictureImage(Width, Height, 1, System.Drawing.Color.Transparent); Imaging.DrawGdPictureImageRect(PageImageID, TempId, 1, 1, Width, Height, (int)Area.Bound.Left, (int)Area.Bound.Top, Width, Height, System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor); //Imaging.SetROI((int)Area.Bound.Left - 10, (int)Area.Bound.Top - 1, (int)Area.Width + 2, (int)Area.Height + 20); string RecognizedValue = Imaging.OCRTesseractDoOCR(TempId, "nld", ATAPY.Common.Application.Path, string.Empty); Imaging.OCRTesseractClear(); //Imaging.SaveAsPNG(TempId, @"d:\Temp\P2P\" + string.Format("Page_{0}_Word_{1}_Cropped.png", Page.Index, i.ToString("D3"))); Imaging.ReleaseGdPictureImage(TempId); //check for words duplication if (RecognizedValue.Length * 2 == Area.Text.Length) { if (Area.Text == RecognizedValue + RecognizedValue) { Area.Text = RecognizedValue; FillCharParams(Area); } } else if (ATAPY.Common.String.MatchRegularExpression(Left, @"^EUR|€|$\d+\s?[.,]?\s?\d*\s?[.,]?\s?\d{2}$")) { Area.Text = Left; FillCharParams(Area); } /*else if (IsEven(Area.Text.Length)) { * var Left = Area.Text.Substring(0, Area.Text.Length / 2); * var Right = Area.Text.Substring(Area.Text.Length / 2); * if (Left == Right && ATAPY.Common.String.MatchRegularExpression(Left, @"^EUR|€|$\d+\s?[.,]?\s?\d*\s?[.,]?\s?\d{2}$")) { * Area.Text = Left; * FillCharParams(Area); * } * }*/ } } }
public static string GerarDocumentoPesquisavelPdf(GdPictureImaging _gdPictureImaging, GdPicturePDF _gdPicturePDF, string documento, bool pdfa = true, string idioma = "por", string titulo = null, string autor = null, string assunto = null, string palavrasChaves = null, string criador = null, int dpi = 250) { if (Path.GetExtension(documento) != ".pdf") { documento = Ocr.castTopdf(documento, _gdPictureImaging, _gdPicturePDF); } int num = 0; var pasta = Guid.NewGuid().ToString(); _gdPicturePDF.LoadFromFile(documento, true); string str = string.Concat(Ocr.GetCurrentDirectory(), "\\GdPicture\\Idiomas"); using (FileStream fs = File.Create("c:\\lodg.txt")) { Byte[] info = new UTF8Encoding(true).GetBytes(str); // Add some information to the file. fs.Write(info, 0, info.Length); } // Console.WriteLine(ex.Message); string str1 = ConfigurationManager.AppSettings["PastaDestinoTemp"].ToString(); string str2 = string.Concat(str1, "\\", Path.GetFileName(documento)); string folder = Guid.NewGuid().ToString(); int pageCount = _gdPicturePDF.GetPageCount(); for (int i = 1; i <= pageCount; i++) { Directory.CreateDirectory(str1 + "\\" + pasta); _gdPicturePDF.SelectPage(i); int gdPictureImageEx = _gdPicturePDF.RenderPageToGdPictureImageEx((float)dpi, true); if (gdPictureImageEx != 0) { num = _gdPictureImaging.PdfOCRStart(str1 + "\\" + pasta + "\\" + i.ToString() + ".pdf", pdfa, titulo, autor, assunto, palavrasChaves, criador); _gdPictureImaging.PdfAddGdPictureImageToPdfOCR(num, gdPictureImageEx, idioma, str, ""); _gdPictureImaging.ReleaseGdPictureImage(gdPictureImageEx); _gdPictureImaging.PdfOCRStop(num); } } _gdPicturePDF.CloseDocument(); File.Delete(documento); GdPictureStatus status = _gdPicturePDF.MergeDocuments(Directory.GetFiles(str1 + "\\" + pasta), str2); DirectoryInfo dir = new DirectoryInfo(str1 + "\\" + pasta); foreach (FileInfo fi in dir.GetFiles()) { fi.Delete(); } Directory.Delete(str1 + "\\" + pasta); return(str2); }
/// <summary> /// Save page to jpeg file /// </summary> /// <param name="sourceFilePath">The source file path.</param> /// <param name="outputPath">The output path.</param> /// <param name="pageNumber">Page number</param> public void SavePageToJpeg(string sourceFilePath, string outputPath, int pageNumber) { using (GdPictureImaging image = new GdPictureImaging()) { int imageId = image.CreateGdPictureImageFromFile(sourceFilePath); image.SelectPage(imageId, pageNumber); image.SaveAsJPEG(imageId, outputPath); image.ReleaseGdPictureImage(imageId); } GC.Collect(); }
private float GetFontSize(GdPictureImaging GdPictureImaging, int ID, TextArea Text, string FontName) { var Height = GdPictureImaging.GetTextHeight(ID, Text.Text, FontName, 50, GdPicture9.FontStyle.FontStyleRegular); var Width = GdPictureImaging.GetTextWidth(ID, Text.Text, FontName, 50, GdPicture9.FontStyle.FontStyleRegular); var Ratio1 = Height / Text.Height; Text.FontSize = 50 / Ratio1; var Ratio2 = Width / Text.Width; return((float)(50 / Math.Max(Ratio1, Ratio2))); }
/// <summary> /// Gets total pages number in file /// </summary> /// <param name="filePath">The file path.</param> /// <returns></returns> public int GetTotalPages(string filePath) { int totalPages = 0; using (GdPictureImaging image = new GdPictureImaging()) { int imageId = image.CreateGdPictureImageFromFile(filePath); totalPages = image.TiffGetPageCount(imageId); totalPages = totalPages > 0 ? totalPages : 1; } return(totalPages); }
/// <summary> /// Save page to jpeg file /// </summary> /// <param name="sourceFilePath">The source file path.</param> /// <param name="outputPath">The output path.</param> /// <param name="pageNumber">Page number</param> /// <param name="dpi">Dpi of the generated file</param> public void SavePageToJpeg(string sourceFilePath, string outputPath, int pageNumber, int dpi) { using (GdPicturePDF pdf = new GdPicturePDF()) { pdf.LoadFromFile(sourceFilePath, false); pdf.SelectPage(pageNumber); int imageId = pdf.RenderPageToGdPictureImage(200, true); using (GdPictureImaging image = new GdPictureImaging()) { image.SaveAsJPEG(imageId, outputPath); image.ReleaseGdPictureImage(imageId); } } }
/// <summary> /// Creates the thumbnail. /// </summary> /// <param name="outputPath">The output path.</param> /// <param name="width">Width.</param> /// <param name="height">Height.</param> public void CreateThumbnail(string outputPath, int width, int height) { GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromFile(ImagePath); if (imageId > 0) { int thumbnailId = image.CreateThumbnailHQ(imageId, width, height, Color.Black); image.SaveAsJPEG(thumbnailId, outputPath); image.ReleaseGdPictureImage(imageId); } GarbageCollector.Collect(); }
private void FillPageText(string PageText, GdPicturePDF SourcePDF, Page Page) { GdPictureImaging GdPictureImaging = new GdPictureImaging(); var Words = PageText.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); SourcePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); SourcePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitPoint); for (int i = 0; i < Words.Length; i++) { string WordSet = Words[i]; try { var Coords = WordSet.Split(SEPARATOR[0]); var Word = CorrectWord(Coords[8]); if (!string.IsNullOrEmpty(Word) && !string.IsNullOrWhiteSpace(Word)) { E_TextOrientation Orientation; System.Windows.Rect Rect = GetRect(Coords, out Orientation); if (Rect.IsEmpty) { continue; } //var asdf = SourcePDF.GetPageTextArea(0, 0, 500, 500); //var AltText = SourcePDF.GetPageTextArea((float)(Rect.Left / Scale / 72.0), (float)(Rect.Top / Scale / 72.0), (float)(Rect.Width / Scale / 72.0), (float)(Rect.Height / Scale / 72.0)); TextArea Area = new TextArea(Rect, Word, Page); Area.Orientation = Orientation; //SetPDFFontSize(SourcePDF, Area, Fonts); if (Orientation == E_TextOrientation.LeftRight) { Page.TextAreas.Add(Area); FillCharParams(Area); } } } catch (Exception ex) { throw ex; } } int ID = GdPictureImaging.CreateNewGdPictureImage((int)Page.Width, (int)Page.Height, 24, System.Drawing.Color.White); foreach (var item in Page.TextAreas) { //calc sizes var FontSize = GetFontSize(GdPictureImaging, ID, item, "Arial"); /*if (item.Text == "-") { * var a = item.FontSize; * }*/ } GdPictureImaging.ReleaseGdPictureImage(ID); }
public TesseractProcessor() { GdImg = new GdPictureImaging(); GdOcr = new GdPictureOCR { EnablePreprocessing = false, OCRMode = OCRMode.FavorAccuracy, Context = OCRContext.OCRContextSingleWord, CharacterSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", ResourceFolder = @"C:\GdPicture.NET 14\Redist\OCR" }; blobFactory = new SpecialFactory(); }
static ColorMap[] GetBoxTable(GdPictureImaging GdPic, int id, IBox box) { var converter = new ColorConverter(); Color cIn = (Color)converter.ConvertFromString("#613e32"); Color cOut = (Color)converter.ConvertFromString("#6c4b3d"); Color cBkg = (Color)converter.ConvertFromString("#fff6d2"); int wi = GdPic.GetWidth(id); int hi = GdPic.GetHeight(id); var result = new List <ColorMap>(); for (int w = 0; w < wi; w++) { for (int h = 0; h < hi; h++) { Color c = GdPic.PixelGetColor(id, w, h); ColorMap map = new ColorMap(); if (box.IsBkg(c)) { map.OldColor = c; map.NewColor = cBkg; } else if (box.IsIn(c)) { map.OldColor = c; map.NewColor = cIn; } else { map.OldColor = c; map.NewColor = cOut; } if (result.Contains(map)) { continue; } result.Add(map); } } return(result.ToArray()); }
/// <summary> /// Set gamma for specified bitmap /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="gamma">Gamma (values from -100 to 100)</param> /// <returns></returns> public static Bitmap SetGammaCorrection(Bitmap bitmap, int gamma) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); if (imageId > 0) { image.SetGammaCorrection(imageId, gamma); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Set contrast for specified bitmap /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="contrast">Contrast (values from -100 to 100)</param> /// <returns></returns> public static Bitmap SetContrast(Bitmap bitmap, int contrast) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); if (imageId > 0) { image.SetContrast(imageId, contrast); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Binarizes the specified bitmap /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="threshold">Threshold</param> /// <returns></returns> public static Bitmap Binarize(Bitmap bitmap, byte threshold) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); if (imageId > 0) { image.ConvertTo1Bpp(imageId, threshold); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Crops the specified bitmap /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="rectangle">Rectangle</param> /// <returns></returns> public static Bitmap Crop(Bitmap bitmap, Rectangle rectangle) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); if (imageId > 0) { image.Crop(imageId, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Removes lines /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="orientation">Lines orientation</param> /// <returns></returns> public static Bitmap RemoveLines(Bitmap bitmap, Enums.LineRemoveOrientation orientation) { Bitmap resultImage = bitmap; LineRemoveOrientation lineRemoveOrientation; switch (orientation) { case Enums.LineRemoveOrientation.Horizontal: lineRemoveOrientation = LineRemoveOrientation.Horizontal; break; case Enums.LineRemoveOrientation.Vertical: lineRemoveOrientation = LineRemoveOrientation.Vertical; break; default: return(resultImage); } if (orientation != Enums.LineRemoveOrientation.None) { GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); try { if (imageId > 0) { if (image.GetBitDepth(imageId) != 1) { image.ConvertTo1BppAT(imageId); } image.RemoveLines(imageId, lineRemoveOrientation); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); } } finally { image.ReleaseGdPictureImage(imageId); } } return(resultImage); }
public static File GetTiff(File file) { var filename = GetFileName(); var compression = TiffCompression.TiffCompressionNONE; int dpi = 300; int tiffId = 0; var status = GdPictureStatus.OK; using (var pdf = new GdPicturePDF()) { using (var imaging = new GdPictureImaging()) { if (pdf.LoadFromFile(file.FullPath, false) == GdPictureStatus.OK) { var pagesCount = pdf.GetPageCount(); for (int i = 1; i <= pagesCount; i++) { if (status == GdPictureStatus.OK) { pdf.SelectPage(i); var pageId = pdf.RenderPageToGdPictureImageEx(dpi, true); if (pageId == 0) { throw new Exception($"Cannot converp page {i}"); } if (i == 1) { tiffId = pageId; status = imaging.TiffSaveAsMultiPageFile(tiffId, filename, compression); } else { status = imaging.TiffAddToMultiPageFile(tiffId, pageId); imaging.ReleaseGdPictureImage(pageId); } } } if (imaging.TiffCloseMultiPageFile(tiffId) != GdPictureStatus.OK) { throw new Exception($"Cannot save file {filename}"); } imaging.ReleaseGdPictureImage(tiffId); } } } return(new File(filename)); }
/// <summary> /// Removes the holes from holemaker /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="punchHolesPosition">Holes position</param> /// <returns></returns> public static Bitmap RemovePunchHoles(Bitmap bitmap, PunchHolesPositions punchHolesPosition) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); try { if (imageId > 0) { if (image.GetBitDepth(imageId) != 1) { image.ConvertTo1BppAT(imageId); } if (punchHolesPosition.LeftSide) { image.RemoveHolePunch(imageId, HolePunchMargins.MarginLeft); } if (punchHolesPosition.TopSide) { image.RemoveHolePunch(imageId, HolePunchMargins.MarginTop); } if (punchHolesPosition.RightSide) { image.RemoveHolePunch(imageId, HolePunchMargins.MarginRight); } if (punchHolesPosition.BottomSide) { image.RemoveHolePunch(imageId, HolePunchMargins.MarginBottom); } resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); } } catch (Exception e) { } finally { image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Binarizes the specified bitmap /// </summary> /// <param name="bitmap">Bitmap</param> /// <returns></returns> public static Bitmap Binarize(Bitmap bitmap) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); if (imageId > 0) { image.FxBlackNWhite(imageId, BitonalReduction.Stucki); image.ConvertTo1BppFast(imageId); resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); image.ReleaseGdPictureImage(imageId); } return(resultImage); }
/// <summary> /// Releases file from the Memory & Closes the file /// </summary> /// <param name="e"></param> public static void HandleCloseDocument(CustomActionEventArgs e) { // Release from memory int imgId; if (e.docuVieware.GetNativeImage(out imgId) == GdPictureStatus.OK) { GdPictureImaging gdImg = new GdPictureImaging(); gdImg.ReleaseGdPictureImage(imgId); } // Close the document e.docuVieware.Close(); return; }
/// <summary> /// Convert the provided TIFF to a PDF, including conversion of the annotations. /// </summary> /// <param name="strOutputFilePath">The path to the Pdf.</param> /// <param name="errorMessage">Used to retrieve an error message.</param> /// <param name="strInputFilePath">The path to the tiff.</param> /// <param name="textExtension">The extension for the text file.</param> /// <returns>true if the conversion succeded otherwise returns false.</returns> public static bool Convert(string strOutputFilePath, ref string errorMessage, string strInputFilePath, string textExtension = "") { using (GdPictureImaging gdPictureImaging = new GdPictureImaging()) { // Load the image int imageId = gdPictureImaging.CreateGdPictureImageFromFile(strInputFilePath); if (imageId == 0) { errorMessage = System.Convert.ToString(gdPictureImaging.GetStat().ToString()); gdPictureImaging.ReleaseGdPictureImage(imageId); return(false); } int pageCount = gdPictureImaging.GetPageCount(imageId); // Collect the information required for the conversion of the annotations PageInfo[] pageInfo = ReadPageInfo(gdPictureImaging, imageId, pageCount); using (MemoryStream stream = new MemoryStream()) { // Convert to Pdf in a stream if (!ConvertToPdf(stream, ref errorMessage, gdPictureImaging, imageId, pageCount)) { gdPictureImaging.ReleaseGdPictureImage(imageId); return(false); } gdPictureImaging.ReleaseGdPictureImage(imageId); // Writes the annotations to the pdf if ( !WriteAnnotations(strOutputFilePath, ref errorMessage, stream, pageCount, pageInfo, textExtension)) { return(false); } } return(true); } }
/// <summary> /// Set sharpness for specified bitmap /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="sharpness">Sharpness</param> /// <returns></returns> public static Bitmap SetSharpness(Bitmap bitmap, int sharpness) { Bitmap resultImage = bitmap; GdPictureImaging image = new GdPictureImaging(); int imageId = image.CreateGdPictureImageFromBitmap(bitmap); if (imageId > 0) { for (int i = 0; i < sharpness; i++) { image.FxSharpen(imageId); } resultImage = image.GetBitmapFromGdPictureImage(imageId).CloneSmart(image.GetPixelFormat(imageId)); image.ReleaseGdPictureImage(imageId); } return(resultImage); }