//------------------------------------------------------------------------- private void SaveFrameFiles(string fileName, Audit sourceAudit, Bitmap entryBitmap, int pageNumber, int pageCount) { if (workGetFramesFromPic) return; workGetFramesFromPic = true; try { var tempdir = OcrAppConfig.TempFolder + "SaveFrame\\"; iOHelper.CreateDirectory(tempdir); var temp = tempdir + sourceAudit.sourceSHA1Hash + "_" + pageNumber + ".tiff"; //iOHelper.DeleteFile(temp); string[] ss = Directory.GetFiles(tempdir); for (int j = 0; j < ss.Length; j++) { try { iOHelper.DeleteFile(ss[j]); } catch (Exception) { } } //Bitmap bmp = (Bitmap)entryBitmap.Clone();// new Bitmap(entryBitmap.Width, entryBitmap.Height, PixelFormat.Format24bppRgb); //bmp = (Bitmap)bmp.GetThumbnailImage(bmp.Width, bmp.Height, null, IntPtr.Zero); //bmp.SetResolution(entryBitmap.VerticalResolution, entryBitmap.HorizontalResolution); //bmp.Save("SaveFrameFiles.bmp", ImageFormat.Bmp); entryBitmap.Save(temp, ImageFormat.Tiff); //bmp.Save(temp, ImageFormat.Tiff); //bmp.Dispose(); var sha1Hash = utils.GetSHA1FromFile(temp); var tempFileName = OcrAppConfig.TempFramesFolder + sha1Hash + ".tiff"; var tempFileNameAudit = OcrAppConfig.TempFramesFolder + sha1Hash + ".audit"; if (File.Exists(tempFileName) && File.Exists(tempFileNameAudit)) { log.LogMessage("Frame exists page " + pageNumber.ToString() + " of " + pageCount.ToString() + " from " + sourceAudit.sourceFileName); iOHelper.DeleteFile(temp); //iOHelper.DeleteDirectory(tempdir, false); workGetFramesFromPic = false; return; } var audit = sourceAudit.GetFrameAudit(tempFileName, sha1Hash, pageNumber); audit.Save(tempFileNameAudit); log.LogMessage("Get frame page " + pageNumber.ToString() + " of " + pageCount.ToString() + " from " + sourceAudit.sourceFileName); //File.Move(temp, tempFileName); File.Copy(temp, tempFileName, true); //System.Windows.Forms.Application.DoEvents(); //iOHelper.DeleteFile(temp); //iOHelper.DeleteDirectory(tempdir, false); //entryBitmap.Dispose();//недопустимый параметр workGetFramesFromPic = false; } catch (Exception) { workGetFramesFromPic = false; } }
//------------------------------------------------------------------------- private void GetFramesFromPic(string fileName, Audit sourceAudit, out Exception exception) { exception = null; if (workGetFramesFromPic) return; workGetFramesFromPic = true; Bitmap entryBitmap; try { entryBitmap = GetPicFile(fileName); } catch (Exception ex) { exception = ex; workGetFramesFromPic = false; return; } int i = 0; try { Guid[] guids = entryBitmap.FrameDimensionsList; FrameDimension fd = new FrameDimension(guids[0]); PageCount = entryBitmap.GetFrameCount(fd); for (i = 0; i < PageCount; i++) { entryBitmap.SelectActiveFrame(fd, i); //SaveFrameFiles(fileName, sourceAudit, entryBitmap, i + 1, PageCount); var tempdir = OcrAppConfig.TempFolder + "SaveFrame\\"; iOHelper.CreateDirectory(tempdir); string[] ss = Directory.GetFiles(tempdir); for (int j = 0; j < ss.Length; j++) { try { iOHelper.DeleteFile(ss[j]); } catch (Exception) { } } var temp = tempdir + sourceAudit.sourceSHA1Hash + "_" + i + 1 + ".tiff"; //iOHelper.DeleteFile(temp); //Bitmap bmp = (Bitmap)entryBitmap.Clone();// new Bitmap(entryBitmap.Width, entryBitmap.Height, PixelFormat.Format24bppRgb); //bmp = (Bitmap)bmp.GetThumbnailImage(bmp.Width, bmp.Height, null, IntPtr.Zero); //bmp.SetResolution(entryBitmap.VerticalResolution, entryBitmap.HorizontalResolution); //bmp.Save("SaveFrameFiles.bmp", ImageFormat.Bmp); entryBitmap.Save(temp, ImageFormat.Tiff); //bmp.Save(temp, ImageFormat.Tiff); //bmp.Dispose(); var sha1Hash = utils.GetSHA1FromFile(temp); //проверить на ""!!! var tempFileName = OcrAppConfig.TempFramesFolder + sha1Hash + ".tiff"; var tempFileNameAudit = OcrAppConfig.TempFramesFolder + sha1Hash + ".audit"; log.LogMessage("Get frame page " + (i + 1).ToString() + " of " + pageCount.ToString() + " from " + sourceAudit.sourceFileName); if (File.Exists(tempFileName) && File.Exists(tempFileNameAudit)) { log.LogMessage("Frame exists page " + (i + 1).ToString() + " of " + pageCount.ToString() + " from " + sourceAudit.sourceFileName); try { iOHelper.DeleteFile(temp); } catch (Exception) { } //iOHelper.DeleteDirectory(tempdir, false); //entryBitmap.Dispose(); //return; continue; } var audit = sourceAudit.GetFrameAudit(tempFileName, sha1Hash, i + 1); audit.Save(tempFileNameAudit); //File.Move(temp, tempFileName); File.Copy(temp, tempFileName, true); //System.Windows.Forms.Application.DoEvents(); //try //{ // iOHelper.DeleteFile(temp); //} //catch (Exception ex) //{ // log.LogMessage(fileName + " Page" + (i + 1).ToString() + " (for) ", ex); // continue; //} //iOHelper.DeleteDirectory(tempdir, false); //entryBitmap.Dispose();//недопустимый параметр } entryBitmap.Dispose(); workGetFramesFromPic = false; } catch (Exception ex) { workGetFramesFromPic = false; entryBitmap.Dispose(); log.LogMessage(fileName + " Page" + (i + 1).ToString() + " ", ex); } }