protected override void OnFormClosed(FormClosedEventArgs e) { if (!DesignMode) { if (_imageInfo != null) { _imageInfo.Dispose(); } _rasterCodecsInstance.Dispose(); } base.OnFormClosed(e); }
public void InitDialog(string fileName, RasterCodecs codecs) { _lstAttachments.View = View.Details; _fileName = fileName; _codecs = codecs; foreach (AttachmentColumnHeader columnHeader in _attachmentColumnHeaders) { _lstAttachments.Columns.Add(columnHeader._columnName, columnHeader._columnWidth, HorizontalAlignment.Left); } CodecsAttachments attachments = codecs.ReadAttachments(fileName); int attachmentNumber = 0; foreach (CodecsAttachment attachment in attachments) { attachmentNumber++; double fileSize = attachment.FileLength / 1024.0; string tempPath = Path.GetTempPath(); string tempAttachmentFile = Path.Combine(Path.GetTempPath(), string.Format("LT_CS_{0}.tmp", attachment.FileName)); ExtractAttachmentFile(tempAttachmentFile, attachmentNumber); CodecsImageInfo info = null; try { info = codecs.GetInformation(tempAttachmentFile, true); } catch { } ListViewItem item = new ListViewItem(new[] { attachment.DisplayName, (info != null) ? ((info.AttachmentCount > 0) ? "Yes" : "No") : "No", attachment.TimeModified.ToString(), (info != null) ? info.IsPortfolio.ToString() : "No", fileSize.ToString("N") + " KB", attachment.Description }); _lstAttachments.Items.Add(item); if (File.Exists(tempAttachmentFile)) { File.Delete(tempAttachmentFile); } if (info != null) { info.Dispose(); } } }
private void _mnuFileSave_Click(object sender, EventArgs e) { if (_lstImagesInfoMulti == null || _lstImagesInfoMulti.Count <= 0) { Messager.ShowError(this, "Please Load Files"); return; } RasterImage img = null; Cursor cursor = this.Cursor; try { cursor = this.Cursor; this.Cursor = Cursors.WaitCursor; EnableControls(false); this._labelConverted.Text = "0"; if (_saveDialog.ShowDialog() == DialogResult.OK) { RasterImageFormat selectedformat = RasterImageFormat.CcittGroup4; if (_saveDialog.FilterIndex == 2) { selectedformat = RasterImageFormat.RasPdfG4; } _szMultiPageFile = _saveDialog.FileName; if (_mnuOptionsSingleRasterImage.Checked) { for (int i = 0; i < _lstImagesInfoMulti.Count; i++) { RasterImage loadedImage = _codecs.Load(_lstImagesInfoMulti[i].Name, 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, 1, -1); if (i == 0) { img = loadedImage; } else { img.AddPages(loadedImage, 1, -1); } _labelConverted.Text = (Convert.ToInt32(_labelConverted.Text) + 1).ToString(); Application.DoEvents(); } _codecs.Save(img, _szMultiPageFile, selectedformat, 0); img.Dispose(); } else { for (int i = 0; i < _lstImagesInfoMulti.Count; i++) { CodecsImageInfo info = _codecs.GetInformation(_lstImagesInfoMulti[i].Name, true); for (int j = 1; j <= info.TotalPages; j++) { img = _codecs.Load(_lstImagesInfoMulti[i].Name, 0, CodecsLoadByteOrder.BgrOrGrayOrRomm, j, j); if (i == 0 && j == 1) { _codecs.Save(img, _szMultiPageFile, selectedformat, 0, 1, 1, 1, CodecsSavePageMode.Overwrite); } else { while (!ReadyToAccess(_szMultiPageFile))//Insure File is not inused by other processes { Application.DoEvents(); } _codecs.Save(img, _szMultiPageFile, selectedformat, 0, 1, 1, -1, CodecsSavePageMode.Append); } img.Dispose(); Application.DoEvents(); } _labelConverted.Text = (Convert.ToInt32(_labelConverted.Text) + 1).ToString(); info.Dispose(); Application.DoEvents(); } } Messager.ShowInformation(this, "Save in MultiPageFile Done"); } } catch (Exception ex) { Messager.ShowError(this, ex); } finally { this.Cursor = cursor; EnableControls(true); } }
/// <summary> /// Load a new image /// </summary> private void _miFileOpen_Click(object sender, System.EventArgs e) { RasterCodecs _codecs = null; CodecsImageInfo imageInfo = null; try { // initialize the codecs object. _codecs = new RasterCodecs(); // Since we are dealing with large images, we do not want to allocate the entire image. We are only going to load it row by row _codecs.Options.Load.AllocateImage = false; _codecs.Options.Load.StoreDataInImage = false; _codecs.LoadImage += new EventHandler <CodecsLoadImageEventArgs>(codecs_LoadImage); // load the image OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Open File"; openFileDialog.Multiselect = false; if (openFileDialog.ShowDialog() == DialogResult.OK) { //Check if image is valid for this demo imageInfo = _codecs.GetInformation(openFileDialog.FileName, false); if (!IsImageValid(imageInfo)) { Messager.ShowError(this, "The input image has to be 8-bit Gray scale, 12-bit Gray scale, 16-bit Gray scale, RGB (color), and TopLeft view perspective." + " This DEMO is not meant to be used with small or palletized images (like GIF, PNG, or 1-bit images)." + " Use this DEMO to save large dimension images efficiently using JPEG2000 compression."); return; } using (RasterSaveDialog saveDialog = new RasterSaveDialog(_codecs)) { saveDialog.AutoProcess = false; saveDialog.Title = "Save As"; saveDialog.ShowFileOptionsBasicJ2kOptions = true; saveDialog.ShowFileOptionsJ2kOptions = true; saveDialog.ShowOptions = true; saveDialog.ShowQualityFactor = true; saveDialog.ShowFileOptionsProgressive = true; saveDialog.ShowFileOptionsStamp = true; saveDialog.QualityFactor = 20; SetupFormats(saveDialog); if (saveDialog.ShowDialog(this) != DialogResult.OK) { return; } _lblFilenameValue.Text = Path.GetFileName(openFileDialog.FileName); _lblDimensionValue.Text = String.Format("{0} x {1}", imageInfo.Width, imageInfo.Height); _lblImageBitsPerPixelValue.Text = imageInfo.BitsPerPixel.ToString(); //Get the selected compression type CodecsCompression selectedCompression; if (saveDialog.Format == RasterImageFormat.J2k) { selectedCompression = CodecsCompression.J2k; } else { selectedCompression = CodecsCompression.Jp2; } RasterByteOrder rasterByteOrder = ((saveDialog.BitsPerPixel == 12) || (saveDialog.BitsPerPixel == 16)) ? RasterByteOrder.Gray : RasterByteOrder.Bgr; CodecsLoadByteOrder codecsLoadByteOrder = ((saveDialog.BitsPerPixel == 12) || (saveDialog.BitsPerPixel == 16)) ? CodecsLoadByteOrder.Gray : CodecsLoadByteOrder.Bgr; bytesPerLine = CalculateBytesPerLine(saveDialog.BitsPerPixel, imageInfo.Width); _codecs.Options.Jpeg.Save.QualityFactor = saveDialog.QualityFactor; _codecs.Options.Jpeg.Save.Passes = saveDialog.Passes; _codecs.Options.Jpeg.Save.SaveWithStamp = saveDialog.WithStamp; _codecs.Options.Jpeg.Save.StampWidth = saveDialog.StampWidth; _codecs.Options.Jpeg.Save.StampHeight = saveDialog.StampHeight; _codecs.Options.Jpeg.Save.StampBitsPerPixel = saveDialog.StampBitsPerPixel; _codecs.Options.Jpeg2000.Save.CompressionControl = saveDialog.FileJ2kOptions.CompressionControl; _codecs.Options.Jpeg2000.Save.CompressionRatio = saveDialog.FileJ2kOptions.CompressionRatio; _codecs.Options.Jpeg2000.Save.DecompositionLevels = saveDialog.FileJ2kOptions.DecompositionLevels; _codecs.Options.Jpeg2000.Save.DerivedQuantization = saveDialog.FileJ2kOptions.DerivedQuantization; _codecs.Options.Jpeg2000.Save.ImageAreaHorizontalOffset = saveDialog.FileJ2kOptions.ImageAreaHorizontalOffset; _codecs.Options.Jpeg2000.Save.ImageAreaVerticalOffset = saveDialog.FileJ2kOptions.ImageAreaVerticalOffset; _codecs.Options.Jpeg2000.Save.ProgressingOrder = saveDialog.FileJ2kOptions.ProgressingOrder; _codecs.Options.Jpeg2000.Save.ReferenceTileHeight = saveDialog.FileJ2kOptions.ReferenceTileHeight; _codecs.Options.Jpeg2000.Save.ReferenceTileWidth = saveDialog.FileJ2kOptions.ReferenceTileWidth; _codecs.Options.Jpeg2000.Save.RegionOfInterest = saveDialog.FileJ2kOptions.RegionOfInterest; _codecs.Options.Jpeg2000.Save.RegionOfInterestRectangle = saveDialog.FileJ2kOptions.RegionOfInterestRectangle; _codecs.Options.Jpeg2000.Save.RegionOfInterestWeight = saveDialog.FileJ2kOptions.RegionOfInterestWeight; _codecs.Options.Jpeg2000.Save.TargetFileSize = saveDialog.FileJ2kOptions.TargetFileSize; _codecs.Options.Jpeg2000.Save.TileHorizontalOffset = saveDialog.FileJ2kOptions.TileHorizontalOffset; _codecs.Options.Jpeg2000.Save.TileVerticalOffset = saveDialog.FileJ2kOptions.TileVerticalOffset; _codecs.Options.Jpeg2000.Save.UseColorTransform = saveDialog.FileJ2kOptions.UseColorTransform; _codecs.Options.Jpeg2000.Save.UseEphMarker = saveDialog.FileJ2kOptions.UseEphMarker; _codecs.Options.Jpeg2000.Save.UseRegionOfInterest = saveDialog.FileJ2kOptions.UseRegionOfInterest; _codecs.Options.Jpeg2000.Save.UseSopMarker = saveDialog.FileJ2kOptions.UseSopMarker; bCancel = false; bCompressing = true; UpdateMyControls(); //Start Compressing using (outputFile = File.Create(saveDialog.FileName)) { _codecs.StartCompress(imageInfo.Width, imageInfo.Height, saveDialog.BitsPerPixel, rasterByteOrder, RasterViewPerspective.TopLeft, bytesPerLine, IntPtr.Zero, 0, selectedCompression, MyCodecsCompressDataCallback); _codecs.Load(openFileDialog.FileName, saveDialog.BitsPerPixel, codecsLoadByteOrder, 1, 1); _codecs.StopCompress(); _lblStatusValue.Text = bCancel ? "Aborted" : "Complete"; } } } } catch (Exception ex) { Messager.ShowError(this, ex); _lblStatusValue.Text = "Error"; } finally { if (_codecs != null) { _codecs.Dispose(); } if (imageInfo != null) { imageInfo.Dispose(); } bCompressing = false; UpdateMyControls(); } }
//Use this load to load an image using the open dialog public int Load(IWin32Window owner, RasterCodecs codecs, bool autoLoad) { using (RasterOpenDialog ofd = new RasterOpenDialog(codecs)) { ofd.DereferenceLinks = true; ofd.CheckFileExists = false; ofd.CheckPathExists = true; ofd.EnableSizing = true; ofd.Filter = Filters; ofd.FilterIndex = _filterIndex; ofd.LoadFileImage = false; ofd.LoadOptions = false; ofd.LoadRotated = true; ofd.LoadCompressed = true; ofd.LoadMultithreaded = codecs.Options.Jpeg.Load.Multithreaded; ofd.ShowLoadMultithreaded = true; ofd.Multiselect = _multiSelect; ofd.ShowGeneralOptions = true; ofd.ShowLoadCompressed = true; ofd.ShowLoadOptions = true; ofd.ShowLoadRotated = true; ofd.ShowMultipage = true; ofd.UseGdiPlus = UseGdiPlus; ofd.ShowPdfOptions = ShowPdfOptions; ofd.ShowXpsOptions = ShowXpsOptions; ofd.ShowXlsOptions = ShowXlsOptions; ofd.ShowRasterizeDocumentOptions = ShowRasterizeDocumentOptions; ofd.EnableFileInfoModeless = true; ofd.EnableFileInfoResizing = true; ofd.ShowVffOptions = ShowVffOptions; ofd.ShowAnzOptions = ShowAnzOptions; ofd.ShowVectorOptions = ShowVectorOptions; ofd.ShowPreview = true; ofd.ShowProgressive = true; ofd.ShowRasterOptions = true; ofd.ShowTotalPages = true; ofd.ShowDeletePage = true; ofd.ShowFileInformation = true; ofd.UseFileStamptoPreview = true; ofd.PreviewWindowVisible = true; ofd.Title = "LEADTOOLS Open Dialog"; ofd.FileName = FileName; ofd.LoadCorrupted = LoadCorrupted; ofd.PreferVector = PreferVector; if (!String.IsNullOrEmpty(_openDialogInitialPath)) { ofd.InitialDirectory = _openDialogInitialPath; } if (ofd.ShowDialog(owner) == DialogResult.OK) { foreach (RasterDialogFileData item in ofd.OpenedFileData) { FileName = item.Name; _filterIndex = ofd.FilterIndex; // Set the RasterizeDocument load options before calling GetInformation codecs.Options.RasterizeDocument.Load.PageWidth = item.Options.RasterizeDocumentOptions.PageWidth; codecs.Options.RasterizeDocument.Load.PageHeight = item.Options.RasterizeDocumentOptions.PageHeight; codecs.Options.RasterizeDocument.Load.LeftMargin = item.Options.RasterizeDocumentOptions.LeftMargin; codecs.Options.RasterizeDocument.Load.TopMargin = item.Options.RasterizeDocumentOptions.TopMargin; codecs.Options.RasterizeDocument.Load.RightMargin = item.Options.RasterizeDocumentOptions.RightMargin; codecs.Options.RasterizeDocument.Load.BottomMargin = item.Options.RasterizeDocumentOptions.BottomMargin; codecs.Options.RasterizeDocument.Load.Unit = item.Options.RasterizeDocumentOptions.Unit; codecs.Options.RasterizeDocument.Load.XResolution = item.Options.RasterizeDocumentOptions.XResolution; codecs.Options.RasterizeDocument.Load.YResolution = item.Options.RasterizeDocumentOptions.YResolution; codecs.Options.RasterizeDocument.Load.SizeMode = item.Options.RasterizeDocumentOptions.SizeMode; if (item.FileInfo.Format == RasterImageFormat.Afp || item.FileInfo.Format == RasterImageFormat.Ptoca) { codecs.Options.Ptoka.Load.Resolution = codecs.Options.RasterizeDocument.Load.XResolution; } // Set the user Options codecs.Options.Load.Passes = item.Passes; codecs.Options.Load.Rotated = item.LoadRotated; codecs.Options.Load.Compressed = item.LoadCompressed; codecs.Options.Load.LoadCorrupted = ofd.LoadCorrupted; codecs.Options.Load.PreferVector = ofd.PreferVector; codecs.Options.Jpeg.Load.Multithreaded = item.LoadMultithreaded; switch (item.Options.FileType) { case RasterDialogFileOptionsType.Meta: { // Set the user options codecs.Options.Wmf.Load.XResolution = item.Options.MetaOptions.XResolution; codecs.Options.Wmf.Load.YResolution = item.Options.MetaOptions.XResolution; break; } case RasterDialogFileOptionsType.Pdf: { if (codecs.Options.Pdf.Load.UsePdfEngine) { // Set the user options codecs.Options.Pdf.Load.DisplayDepth = item.Options.PdfOptions.DisplayDepth; codecs.Options.Pdf.Load.GraphicsAlpha = item.Options.PdfOptions.GraphicsAlpha; codecs.Options.Pdf.Load.TextAlpha = item.Options.PdfOptions.TextAlpha; codecs.Options.Pdf.Load.UseLibFonts = item.Options.PdfOptions.UseLibFonts; } break; } case RasterDialogFileOptionsType.Misc: { switch (item.FileInfo.Format) { case RasterImageFormat.Jbig: { // Set the user options codecs.Options.Jbig.Load.Resolution = new LeadSize(item.Options.MiscOptions.XResolution, item.Options.MiscOptions.YResolution); break; } case RasterImageFormat.Cmw: { // Set the user options codecs.Options.Jpeg2000.Load.CmwResolution = new LeadSize(item.Options.MiscOptions.XResolution, item.Options.MiscOptions.YResolution); break; } case RasterImageFormat.Jp2: { // Set the user options codecs.Options.Jpeg2000.Load.Jp2Resolution = new LeadSize(item.Options.MiscOptions.XResolution, item.Options.MiscOptions.YResolution); break; } case RasterImageFormat.J2k: { // Set the user options codecs.Options.Jpeg2000.Load.J2kResolution = new LeadSize(item.Options.MiscOptions.XResolution, item.Options.MiscOptions.YResolution); break; } } break; } case RasterDialogFileOptionsType.Xls: { // Set the user options codecs.Options.Xls.Load.MultiPageSheet = item.Options.XlsOptions.MultiPageSheet; codecs.Options.Xls.Load.ShowHiddenSheet = item.Options.XlsOptions.ShowHiddenSheet; #if LEADTOOLS_V20_OR_LATER codecs.Options.Xls.Load.MultiPageUseSheetWidth = item.Options.XlsOptions.MultiPageUseSheetWidth; codecs.Options.Xls.Load.PageOrderDownThenOver = item.Options.XlsOptions.PageOrderDownThenOver; codecs.Options.Xls.Load.MultiPageEnableMargins = item.Options.XlsOptions.MultiPageEnableMargins; #endif //#if LEADTOOLS_V20_OR_LATER } break; case RasterDialogFileOptionsType.Vff: { codecs.Options.Vff.Load.View = item.Options.VffOptions.View; break; } case RasterDialogFileOptionsType.Anz: { codecs.Options.Anz.Load.View = item.Options.AnzOptions.View; break; } case RasterDialogFileOptionsType.Vector: { codecs.Options.Vector.Load.BackgroundColor = item.Options.VectorOptions.Options.BackgroundColor; codecs.Options.Vector.Load.BitsPerPixel = item.Options.VectorOptions.Options.BitsPerPixel; codecs.Options.Vector.Load.ForceBackgroundColor = item.Options.VectorOptions.Options.ForceBackgroundColor; codecs.Options.Vector.Load.ViewHeight = item.Options.VectorOptions.Options.ViewHeight; codecs.Options.Vector.Load.ViewMode = item.Options.VectorOptions.Options.ViewMode; codecs.Options.Vector.Load.ViewWidth = item.Options.VectorOptions.Options.ViewWidth; break; } } int firstPage = 1; int lastPage = 1; int infoTotalPages; CodecsImageInfo info = null; using (WaitCursor wait = new WaitCursor()) { info = codecs.GetInformation(FileName, true); infoTotalPages = info.TotalPages; } if (_showLoadPagesDialog) { firstPage = 1; lastPage = infoTotalPages; if (firstPage != lastPage) { using (ImageFileLoaderPagesDialog dlg = new ImageFileLoaderPagesDialog(infoTotalPages, LoadOnlyOnePage)) { if (dlg.ShowDialog(owner) == DialogResult.OK) { firstPage = dlg.FirstPage; lastPage = dlg.LastPage; } else { if (info != null) { info.Dispose(); } return(0); } } } } else { firstPage = item.PageNumber; lastPage = item.PageNumber; } _firstPage = firstPage; _lastPage = lastPage; if (!SetDocumentLoadResultion(codecs, info, firstPage, lastPage)) { info.Dispose(); return(0); } if (autoLoad) { using (WaitCursor wait = new WaitCursor()) { _image = codecs.Load(FileName, 0, CodecsLoadByteOrder.BgrOrGray, firstPage, lastPage); if (codecs.LoadStatus != RasterExceptionCode.Success) { String message = String.Format("The image was only partially loaded due to error: {0}", codecs.LoadStatus.ToString()); Messager.Show(null, message, MessageBoxIcon.Information, MessageBoxButtons.OK); } if (_image != null) { _image.CustomData.Add("IsBigTiff", info.Tiff.IsBigTiff); _images.Add(new ImageInformation(_image, item.Name)); } } } info.Dispose(); } } return(ofd.OpenedFileData.Count); } }