Provides extra options for loading PowerPoint format files.
public class CodecsPowerPointOptions
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing.Core;
// This example shows how to load only visible PowerPoint slides
public void CodecsPowerPointOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.pptx");
string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, "Out.png");
// Set CodecsPowerPointLoadOptions.ShowHiddenSlides to false to load only visible slides
codecs.Options.PowerPoint.Load.ShowHiddenSlides = false; // CodecsPowerPointOptions
// Load the first visible slide
using (RasterImage image = codecs.Load(srcFileName, 1))
{
codecs.Save(image, dstFileName, RasterImageFormat.Png, 0);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsPowerPointOptions-->
/// <!--CodecsPtokaOptions-->
public void CodecsPtokaOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "BusinessForm.ica");
// Setting new options.
// CodecsPtokaOptions & CodecsPtokaLoadOptions reference
codecs.Options.Ptoka.Load.Resolution = 500;
RasterImage image = codecs.Load(srcFileName);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsPtokaOptions-->
/// <!--CodecsRawOptions-->
private struct RawData
{
public int Width; // Width of image
public int Height; // Height of image
public int BitsPerPixel; // Bits per pixel of image--if palettized, a gray palette is generated
public RasterViewPerspective ViewPerspective; // View perspective of raw data (TopLeft, BottomLeft, etc)
public RasterByteOrder Order; // Rgb or Bgr
public int XResolution; // Horizontal resolution in DPI
public int YResolution; // Vertical resolution in DPI
public int Offset; // Offset into file where raw data begins
public bool Padding; // true if each line of data is padded to four bytes
public bool ReverseBits; // true if the bits of each byte are reversed
}
private RawData myRawData;
private void codecs_LoadInformation(object sender, CodecsLoadInformationEventArgs e)
{
// Set the information
e.Format = RasterImageFormat.Raw;
e.Width = myRawData.Width;
e.Height = myRawData.Height;
e.BitsPerPixel = myRawData.BitsPerPixel;
e.XResolution = myRawData.XResolution;
e.YResolution = myRawData.YResolution;
e.Offset = myRawData.Offset;
if (myRawData.Padding)
e.Pad4 = true;
e.Order = myRawData.Order;
if (myRawData.ReverseBits)
e.LeastSignificantBitFirst = true;
e.ViewPerspective = myRawData.ViewPerspective;
// If image is palettized create a grayscale palette
if (e.BitsPerPixel <= 8)
{
int colors = 1 << e.BitsPerPixel;
RasterColor[] palette = new RasterColor[colors];
for (int i = 0; i < palette.Length; i++)
{
byte val = (byte)((i * 255) / (colors - 1));
palette[i] = new RasterColor(val, val, val);
}
e.SetPalette(palette);
}
}
public void CodecsRawOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
// First, load a JPEG or CMP file
RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"));
// Save this image as RAW data
// Set RAW options
// CodecsRawOptions & CodecsRawSaveOptions reference
codecs.Options.Raw.Save.Pad4 = true;
codecs.Options.Raw.Save.ReverseBits = true;
codecs.Options.Save.OptimizedPalette = true;
string rawFileName = Path.Combine(LEAD_VARS.ImagesDir, "Test.raw");
codecs.Save(image, rawFileName, RasterImageFormat.Raw, 0);
// Save information about this image so we can use it to load the RAW file
myRawData = new RawData();
myRawData.Width = image.Width;
myRawData.Height = image.Height;
myRawData.BitsPerPixel = image.BitsPerPixel;
myRawData.ViewPerspective = image.ViewPerspective;
myRawData.Order = image.Order;
myRawData.XResolution = image.XResolution;
myRawData.YResolution = image.YResolution;
myRawData.Offset = 0;
myRawData.Padding = true;
myRawData.ReverseBits = true;
// We dont need the image anymore
image.Dispose();
// Now load this RAW image back
// First subscribe to the LoadInformation event so we can set the image information
codecs.LoadInformation += new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);
// Load the image
image = codecs.Load(rawFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "raw.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Unsubscribe from the event
codecs.LoadInformation -= new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsRawOptions-->
/// <!--CodecsRtfOptions-->
public void CodecsRtfOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.rtf");
// Setting new options.
codecs.Options.RasterizeDocument.Load.LeftMargin = 1.00;
codecs.Options.RasterizeDocument.Load.TopMargin = 1.25;
codecs.Options.RasterizeDocument.Load.PageWidth = 8.50;
codecs.Options.RasterizeDocument.Load.PageHeight = 11.00;
codecs.Options.RasterizeDocument.Load.BottomMargin = 1.00;
codecs.Options.RasterizeDocument.Load.RightMargin = 1.25;
codecs.Options.Rtf.Load.BackColor = RasterColor.White; // CodecsRtfOptions & CodecsRtfLoadOptions reference
RasterImage image = codecs.Load(srcFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "rtf.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsRtfOptions-->
/// <!--CodecsTiffOptions-->
public void CodecsTiffOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "OCR1.TIF");
// Get all Information about the Tiff file that you want to load.
CodecsImageInfo imageInfo = codecs.GetInformation(srcFileName, true);
if (imageInfo.Tiff.IsImageFileDirectoryOffsetValid)
{
codecs.Options.Tiff.Load.ImageFileDirectoryOffset = imageInfo.Tiff.ImageFileDirectoryOffset;
codecs.Options.Tiff.Save.ImageFileDirectoryOffset = imageInfo.Tiff.ImageFileDirectoryOffset;
}
// CodecsTiffOptions & CodecsTiffLoadOptions reference
codecs.Options.Tiff.Load.UseImageFileDirectoryOffset = imageInfo.Tiff.IsImageFileDirectoryOffsetValid;
codecs.Options.Tiff.Save.UseImageFileDirectoryOffset = imageInfo.Tiff.IsImageFileDirectoryOffsetValid;
codecs.Options.Tiff.Load.IgnorePhotometricInterpretation = true;
codecs.Options.Tiff.Load.IgnoreViewPerspective = false;
codecs.Options.Tiff.Load.UseFastConversion = false;
RasterImage image = codecs.Load(srcFileName);
codecs.Options.Tiff.Save.NoPalette = imageInfo.Tiff.HasNoPalette;
codecs.Options.Tiff.Save.PreservePalette = imageInfo.Tiff.HasNoPalette;
// Set the tiffSave options
// CodecsTiffSaveOptions reference
codecs.Options.Tiff.Save.NoPageNumber = false;
codecs.Options.Tiff.Save.NoSubFileType = false;
codecs.Options.Tiff.Save.UsePhotometricInterpretation = true;
codecs.Options.Tiff.Save.PhotometricInterpretation = CodecsTiffPhotometricInterpretation.YcbCr;
codecs.Options.Tiff.Save.UseTileSize = true;
codecs.Options.Tiff.Save.TileHeight = 200;
codecs.Options.Tiff.Save.TileWidth = 200;
codecs.Options.Tiff.Save.NoLzwAutoClear = false;
codecs.Options.Tiff.Save.SavePlanar = false;
codecs.Options.Tiff.Save.UsePredictor = false;
// make the output file have the same format as the input, so Tiff->Tiff, BigTiff->BigTiff
codecs.Options.Tiff.Save.BigTiff = imageInfo.Tiff.IsBigTiff;
//saving the image
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "testtiff1.TIF"), RasterImageFormat.Tif, image.BitsPerPixel, 1, image.PageCount, 1, CodecsSavePageMode.Overwrite);
//change some tiffsave options.
codecs.Options.Tiff.Save.NoPageNumber = true;
codecs.Options.Tiff.Save.NoSubFileType = true;
codecs.Options.Tiff.Save.UsePhotometricInterpretation = false;
codecs.Options.Tiff.Save.UseTileSize = false;
codecs.Options.Tiff.Save.NoPalette = false;
codecs.Options.Tiff.Save.PreservePalette = true;
codecs.Options.Tiff.Save.PhotometricInterpretation = CodecsTiffPhotometricInterpretation.Palette;
//save the image with different name.
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "testtiff2.TIF"), RasterImageFormat.CcittGroup4, image.BitsPerPixel, 1, image.PageCount, 1, CodecsSavePageMode.Overwrite);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsTiffOptions-->
/// <!--CodecsTxtOptions-->
public void CodecsTxtOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.txt");
// Setting new options.
codecs.Options.RasterizeDocument.Load.LeftMargin = 1.00;
codecs.Options.RasterizeDocument.Load.TopMargin = 1.25;
codecs.Options.RasterizeDocument.Load.PageWidth = 8.50;
codecs.Options.RasterizeDocument.Load.PageHeight = 11.00;
codecs.Options.RasterizeDocument.Load.BottomMargin = 1.00;
codecs.Options.RasterizeDocument.Load.RightMargin = 1.25;
// CodecsTxtOptions & CodecsTxtLoadOptions reference
codecs.Options.Txt.Load.BackColor = RasterColor.White;
codecs.Options.Txt.Load.FaceName = "Courier New";
codecs.Options.Txt.Load.FontColor = new RasterColor(255, 0, 0);
codecs.Options.Txt.Load.Highlight = RasterColor.White;
codecs.Options.Txt.Load.FontSize = 12;
codecs.Options.Txt.Load.Italic = false;
codecs.Options.Txt.Load.Strikethrough = false;
codecs.Options.Txt.Load.Underline = false;
codecs.Options.Txt.Load.Bold = true;
codecs.Options.Txt.Load.Enabled = true;
codecs.Options.Txt.Load.TabSize = 3;
RasterImage image = codecs.Load(srcFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "txt.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsTxtOptions-->
/// <!--CodecsWmfOptions-->
public void CodecsWmfOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
// Setting new options.
// CodecsWmfOptions & CodecsWmfLoadOptions reference
codecs.Options.Wmf.Load.XResolution = 500;
codecs.Options.Wmf.Load.YResolution = 500;
codecs.Options.Wmf.Load.Resolution = 0;
RasterImage image = codecs.Load(srcFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "WmfImage.wmf"), RasterImageFormat.Wmf, image.BitsPerPixel);
image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "WmfImage.wmf"));
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "wmf.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsWmfOptions-->
/// <!--CodecsXpsOptions-->
public void CodecsXpsOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.xps");
string pngDestFileName = Path.Combine(LEAD_VARS.ImagesDir, "PngCompressed.xps");
string jpegDestFileName = Path.Combine(LEAD_VARS.ImagesDir, "JpegCompressed.xps");
// Set the resolution for loading XPS files to 300 by 300 DPI (Dots per inch)
// CodecsXpsOptions & CodecsXpsLoadOptions reference
codecs.Options.RasterizeDocument.Load.XResolution = 300;
codecs.Options.RasterizeDocument.Load.YResolution = 300;
// Load the source file
RasterImage image = codecs.Load(srcFileName);
// Save this file as XPS with PNG compression and quality factor of 9 (highest compression)
// CodecsXpsSaveOptions reference
codecs.Options.Xps.Save.PngQualityFactor = 9;
codecs.Save(image, pngDestFileName, RasterImageFormat.Xps, 24);
// Save this file as XPS with JPEG 422 compression and quality factor of 255 (highest compression)
codecs.Options.Xps.Save.JpegQualityFactor = 255;
codecs.Save(image, jpegDestFileName, RasterImageFormat.Xps, 24);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsXpsOptions-->
/// <!--CodecsXlsOptions-->
public void CodecsXlsOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.Xls");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
// Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
rasterizeDocumentLoadOptions.PageWidth = 8.5;
rasterizeDocumentLoadOptions.PageHeight = 11;
rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
rasterizeDocumentLoadOptions.XResolution = 300;
rasterizeDocumentLoadOptions.YResolution = 300;
// Load each sheet in a separate page
// CodecsXlsOptions & CodecsXlsLoadOptions reference
codecs.Options.Xls.Load.MultiPageSheet = true;
codecs.Options.Xls.Load.MultiPageEnableMargins = false;
codecs.Options.Xls.Load.MultiPageUseSheetWidth = false;
codecs.Options.Xls.Load.PageOrderDownThenOver = false;
codecs.Options.Xls.Load.ShowHiddenSheet = false;
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Show the image information
Debug.WriteLine("Image has {0} pages", image.PageCount);
Debug.WriteLine("Image size: {0} by {1} pixels at {2} by {3} DPI",
image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsXlsOptions-->
/// <!--CodecsExcelOptions-->
public void CodecsExcelOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.Xls");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
// Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
rasterizeDocumentLoadOptions.PageWidth = 8.5;
rasterizeDocumentLoadOptions.PageHeight = 11;
rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
rasterizeDocumentLoadOptions.XResolution = 300;
rasterizeDocumentLoadOptions.YResolution = 300;
// Load each sheet in a separate page - CodecsExcelOptions & CodecsExcelLoadOptions reference
codecs.Options.Excel.Load.MultiPageSheet = false;
codecs.Options.Excel.Load.MultiPageEnableMargins = false;
codecs.Options.Excel.Load.MultiPageUseSheetWidth = false;
codecs.Options.Excel.Load.PageOrderDownThenOver = false;
codecs.Options.Excel.Load.ShowHiddenSheets = false; // ShowHiddenSheet reference
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Show the image information
Debug.WriteLine("Image has {0} pages", image.PageCount);
Debug.WriteLine("Image size: {0} by {1} pixels at {2} by {3} DPI",
image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
//To save as BMP, uncomment out this line:
//codecs.Save(image, srcFileName + ".bmp", RasterImageFormat.Bmp, 24);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsExcelOptions-->
/// <!--CodecsRasterizeDocumentLoadOptions-->
public void RasterizeDocumentExample()
{
// Initialize the RasterCodecs object to use
RasterCodecs codecs = new RasterCodecs();
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
// No margins (only used with RTF and TXT files)
rasterizeDocumentLoadOptions.LeftMargin = 0;
rasterizeDocumentLoadOptions.TopMargin = 0;
rasterizeDocumentLoadOptions.RightMargin = 0;
rasterizeDocumentLoadOptions.BottomMargin = 0;
// The PDF file we are testing. This could be an XPS, RTF, TXT or XLS file as well
string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf");
// Show the file information
using (CodecsImageInfo imageInfo = codecs.GetInformation(imageFileName, true))
{
CodecsDocumentImageInfo documentImageInfo = imageInfo.Document;
// If this is a document file, show the document information
if (documentImageInfo.IsDocumentFile)
{
Debug.WriteLine("Document file");
Debug.WriteLine(" Original size is: {0} by {1} {2}",
documentImageInfo.PageWidth, documentImageInfo.PageHeight, documentImageInfo.Unit);
Debug.WriteLine(" Using current rasterization, load size will be: {0} by {1} pixels at {2} by {3}",
imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution);
}
else
{
// Regular raster image, show the image information
Debug.WriteLine("Raster file");
Debug.WriteLine(" Original size is: {0} by {1} pixels at {2} by {3}",
imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution);
}
}
// Example 1. Load at original document size at 300 DPI
rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.None;
rasterizeDocumentLoadOptions.XResolution = 300;
rasterizeDocumentLoadOptions.YResolution = 300;
using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
{
ShowResult(codecs, image);
}
// Example 2. Fit the document at 640 by 480 pixels at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
rasterizeDocumentLoadOptions.PageWidth = 640;
rasterizeDocumentLoadOptions.PageHeight = 480;
rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Pixel;
rasterizeDocumentLoadOptions.XResolution = 96;
rasterizeDocumentLoadOptions.YResolution = 96;
using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
{
ShowResult(codecs, image);
}
// Example 3. Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
rasterizeDocumentLoadOptions.PageWidth = 8.5;
rasterizeDocumentLoadOptions.PageHeight = 11;
rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
rasterizeDocumentLoadOptions.XResolution = 300;
rasterizeDocumentLoadOptions.YResolution = 300;
using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
{
ShowResult(codecs, image);
}
// Example 4. Stretch the image to always be 4 by 4 inches at 150 DPI. Aspect ratio may differ
rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Stretch;
rasterizeDocumentLoadOptions.PageWidth = 4;
rasterizeDocumentLoadOptions.PageHeight = 4;
rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
rasterizeDocumentLoadOptions.XResolution = 150;
rasterizeDocumentLoadOptions.YResolution = 150;
using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
{
ShowResult(codecs, image);
}
codecs.Dispose();
}
private static void ShowResult(RasterCodecs codecs, RasterImage image)
{
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
Debug.WriteLine("Showing result..");
Debug.WriteLine(" Current rasterization options:");
Debug.WriteLine(" Size mode is: {0}", rasterizeDocumentLoadOptions.SizeMode);
Debug.WriteLine(" Page size is {0} by {1} {2}",
rasterizeDocumentLoadOptions.PageWidth, rasterizeDocumentLoadOptions.PageHeight, rasterizeDocumentLoadOptions.Unit);
Debug.WriteLine(" Resolution is {0} by {1}",
rasterizeDocumentLoadOptions.XResolution, rasterizeDocumentLoadOptions.YResolution);
Debug.WriteLine(" Loaded raster image size is: {0} by {1} at {2} by {3}",
image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
// Code to verify our results
// Get the suggested page width and height in pixels
double pageWidthInPixels;
double pageHeightInPixels;
switch (rasterizeDocumentLoadOptions.Unit)
{
case CodecsRasterizeDocumentUnit.Inch:
pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution;
pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution;
break;
case CodecsRasterizeDocumentUnit.Millimeter:
pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution * 25.400000025908000026426160026955;
pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution * 25.400000025908000026426160026955;
break;
case CodecsRasterizeDocumentUnit.Pixel:
default:
pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth;
pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight;
break;
}
// Verify
switch (rasterizeDocumentLoadOptions.SizeMode)
{
case CodecsRasterizeDocumentSizeMode.Fit:
case CodecsRasterizeDocumentSizeMode.FitAlways:
// The image width/height must not be greater than suggested page width/height
Debug.WriteLine("Image width/height must be less than or equal to page width/height");
System.Diagnostics.Debug.Assert(image.ImageWidth <= pageWidthInPixels);
System.Diagnostics.Debug.Assert(image.ImageHeight <= pageHeightInPixels);
break;
case CodecsRasterizeDocumentSizeMode.FitWidth:
// The image width must be equal to the suggested page width
Debug.WriteLine("Image width must be equal to page width");
System.Diagnostics.Debug.Assert(image.ImageWidth == pageWidthInPixels);
break;
case CodecsRasterizeDocumentSizeMode.Stretch:
// The image width/height must be equal to the suggested page width/height
Debug.WriteLine("Image width/height must be equal to suggested page width/height");
System.Diagnostics.Debug.Assert(image.ImageWidth == pageWidthInPixels);
System.Diagnostics.Debug.Assert(image.ImageHeight == pageHeightInPixels);
break;
case CodecsRasterizeDocumentSizeMode.None:
default:
Debug.WriteLine("Loading at original document page size");
// No checking
break;
}
}
/// <!--CodecsRasterizeDocumentLoadOptions-->
/// <!--CodecsDocLoadOptions-->
public void CodecsDocLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.doc");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
// Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
rasterizeDocumentLoadOptions.PageWidth = 8.5;
rasterizeDocumentLoadOptions.PageHeight = 11;
rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
rasterizeDocumentLoadOptions.XResolution = 300;
rasterizeDocumentLoadOptions.YResolution = 300;
// Load each page at 1-bits/pixel
codecs.Options.Doc.Load.BitsPerPixel = 1; // CodecsDocLoadOptions & CodecsDocOptions references
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Show the image information
Debug.WriteLine("Image has {0} pages", image.PageCount);
Debug.WriteLine("Image size: {0} by {1} pixels at {2} by {3} DPI",
image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsDocLoadOptions-->
/// <!--CodecsAnzLoadOptions-->
public void CodecsAnzLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.Anz");
string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_AnzCoronal.bmp");
// Set the load options to use
// "view is from front" ANZ viewing options
codecs.Options.Anz.Load.View = CodecsAnzView.Coronal; // CodecsAnzOptions & CodecsAnzLoadOptions reference
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Save the image as BMP
codecs.Save(image, dstFileName, RasterImageFormat.Bmp, 24);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsAnzLoadOptions-->
/// <!--CodecsVffLoadOptions-->
public void CodecsVffLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.vff");
string dstFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Vff_UpToDown.bmp");
string dstFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Vff_LeftToRight.bmp");
// Set the load options to use up to down along the X-Axis
// CodecsVffOptions & CodecsVffLoadOptions reference
codecs.Options.Vff.Load.View = CodecsVffView.UpToDown;
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Save the image as BMP
codecs.Save(image, dstFileName1, RasterImageFormat.Bmp, 24);
}
// Now, set the load options to use left to right along the Y-Axis
codecs.Options.Vff.Load.View = CodecsVffView.LeftToRight;
// Re-load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Save the image as BMP
codecs.Save(image, dstFileName2, RasterImageFormat.Bmp, 24);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsVffLoadOptions-->
/// <!--CodecsRtfOptions.LoadMetafile-->
[DllImport("Gdi32")]
private static extern int DeleteEnhMetaFile(IntPtr hemf);
public void LoadRtfAsMetafileExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.rtf");
// Load the source file as a metafile
IntPtr hemf = codecs.Options.Rtf.LoadMetafile(srcFileName, 1);
if (hemf != IntPtr.Zero)
{
// Use the metafile here
// Finally, delete it using the Windows API
DeleteEnhMetaFile(hemf);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsRtfOptions.LoadMetafile-->
/// <!--CodecsSaveOptions.RetrieveDataFromImage-->
private int myMode;
public void RetrieveDataFromImageExample()
{
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp");
string blankFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Blank.bmp");
string defaultFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Default.bmp");
string invertedFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Inverted.bmp");
// Load the source image
using (RasterImage image = codecs.Load(srcFile))
{
// Subscribe to the SaveImage event
codecs.SaveImage += new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);
// First, set RetrieveDataFromImage to false and save
// This should generate a blank image
myMode = 0;
codecs.Options.Save.RetrieveDataFromImage = false;
codecs.Save(image, blankFile, RasterImageFormat.Bmp, 24);
// Next, set RetrieveDataFromImage to true but do not
// change the data, this should generate the correct image and is the equivalant
// to calling Save without subscribing to SaveImage
myMode = 1;
codecs.Options.Save.RetrieveDataFromImage = true;
codecs.Save(image, defaultFile, RasterImageFormat.Bmp, 24);
// Finally, leave RetrieveDataFromImage as true but change the data by inverting
// the scanlines, this should generate an inverted image
myMode = 2;
codecs.Save(image, invertedFile, RasterImageFormat.Bmp, 24);
codecs.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);
}
}
}
private void codecs_SaveImage(object sender, CodecsSaveImageEventArgs e)
{
// This example works with 24 bpp images only
Debug.Assert(e.Image.BitsPerPixel == 24);
switch (myMode)
{
case 0:
// Do not do anything
break;
case 1:
// Do not do anything
break;
case 2:
// Invert the image data
{
int scanlineLength = e.Image.BytesPerLine;
byte[] scanline = new byte[scanlineLength];
// Loop through all the scanlines in the data
for (int y = 0; y < e.Lines; y++)
{
// Get this row
e.Buffer.GetData(y * scanlineLength, scanline, 0, scanlineLength);
// Process it by inverting every pixel data
for (int x = 0; x < scanlineLength; x++)
{
scanline[x] = (byte)(255 - scanline[x]);
}
// Copy it back to the event buffer
e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength);
}
}
break;
}
}
/// <!--CodecsSaveOptions.RetrieveDataFromImage-->
/// <!--CodecsVectorLoadOptions-->
public void CodecsVectorLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "random.dxf");
//Check if it is a vector file
CodecsImageInfo info = codecs.GetInformation(srcFileName, false);
Debug.WriteLine($"Is {srcFileName} a vector file? : {info.Vector.IsVectorFile}");
Debug.WriteLine($"Units: {info.Vector.Unit}");
//Parallelogram data
CodecsVectorImageInfo codecsVectorImageInfo = info.Vector;
Debug.WriteLine($"Parallelogram Max X: {codecsVectorImageInfo.ParallelogramMaxX}");
Debug.WriteLine($"Parallelogram Max Y: {codecsVectorImageInfo.ParallelogramMaxY}");
Debug.WriteLine($"Parallelogram Max Z: {codecsVectorImageInfo.ParallelogramMaxZ}");
Debug.WriteLine($"Parallelogram Min X: {codecsVectorImageInfo.ParallelogramMinX}");
Debug.WriteLine($"Parallelogram Min Y: {codecsVectorImageInfo.ParallelogramMinY}");
Debug.WriteLine($"Parallelogram Min Z: {codecsVectorImageInfo.ParallelogramMinZ}");
// Set the vector load options
// CodecsVectorOptions & CodecsVectorLoadOptions reference
codecs.Options.Vector.Load.BackgroundColor = new RasterColor(255, 255, 255);
codecs.Options.Vector.Load.BitsPerPixel = 24;
codecs.Options.Vector.Load.ForceBackgroundColor = true;
codecs.Options.Vector.Load.ViewWidth = 800;
codecs.Options.Vector.Load.ViewHeight = 800;
codecs.Options.Vector.Load.ViewMode = CodecsVectorViewMode.UseBest;
// Load the image
RasterImage image = codecs.Load(srcFileName);
// do something with the image here
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsVectorLoadOptions-->
/// <!--CodecsPstOptions-->
public void CodecsPstOptionsExample()
{
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "file.pst");
int messageCount = 0;
// Get number of messages in the PST files
using (CodecsImageInfo imageInfo = codecs.GetInformation(srcFileName, true))
{
messageCount = imageInfo.Pst.MessageCount;
}
// Load all messages as pure text
for (int messageNumber = 1; messageNumber <= messageCount; messageNumber++)
{
// Load message number 10 as text only
// CodecsPstOptions & CodecsPstLoadOptions reference
codecs.Options.Pst.Load.MessageNumber = messageNumber;
codecs.Options.Pst.Load.PlainText = true;
// Load the image
using (RasterImage image = codecs.Load(srcFileName))
{
// do something with the image here
}
}
}
}
/// <!--CodecsPstOptions-->
/// <!--RasterImageTypeConverter-->
public void RasterImageTypeConverterExample()
{
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif");
// Construct the source URL to use the source file and load the image
Uri uri = new Uri(srcFileName);
RasterCodecs codecs = new RasterCodecs();
// Create a new instance of the RasterImageTypeConverter class
RasterImageTypeConverter rasterImageConverter = new RasterImageTypeConverter(codecs);
// We should be able to convert from a URL
Debug.Assert(rasterImageConverter.CanConvertFrom(uri.GetType()));
// Convert the image
// The return value from RasterImageTypeConverter.ConvertFrom might be an image that is
// still loading. So, we must subscribe to the RasterCodecs.LoadAsyncCompleteted
// event to obtain the finished image
codecs.LoadAsyncCompleted += new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterImageTypeConverterExample_LoadAsyncCompleted);
object rasterImageObject = rasterImageConverter.ConvertFrom(uri);
// Notice that the returned rasterImageObject is a RasterImage with IsLoading set to true at this point
// The IsLoading will be false (and hence, the object will be usable) when the LoadAsyncCompleteted
// fires.
}
private void rasterImageTypeConverterExample_LoadAsyncCompleted(object sender, CodecsLoadAsyncCompletedEventArgs e)
{
// Check if the user canceled or if we have errors
if (e.Cancelled)
{
Debug.WriteLine("Canceled by the user");
}
else if (e.Error != null)
{
Debug.WriteLine("Error: " + e.Error.Message);
}
else
{
// Everything is OK, get the image
RasterImage image = e.Image;
Debug.WriteLine("Image at {0} loaded OK, size: {1} by {2}", e.Uri, image.Width, image.Height);
image.Dispose();
}
// Unsubscribe to the event and dispose the RasterCodecs object
RasterCodecs codecs = sender as RasterCodecs;
codecs.LoadAsyncCompleted -= new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterImageTypeConverterExample_LoadAsyncCompleted);
codecs.Dispose();
}
/// <!--RasterImageTypeConverter-->
/// <!--CodecsHtmlOptions-->
public void CodecsHtmlOptions_Example()
{
using (RasterCodecs codecs = new RasterCodecs())
{
// Use the default HTML rendering engine - CodecsHtmlOptions & CodecsHtmlLoadOptions reference
codecs.Options.Html.Load.HtmlEngine = CodecsHtmlEngine.Auto;
// Set the HTML options to only allow loading resources from the leadtools.com domain:
codecs.Options.Html.Load.DomainWhitelist = "leadtools.com";
// Disable all JavaScript embedded in the HTML file
codecs.Options.Html.Load.EnableJS = false;
// Load the image
using (RasterImage image = codecs.Load("file.html", 1))
{
// Do something with image
}
}
}
/// <!--CodecsHtmlOptions-->
/// <!--CodecsCsvOptions-->
public void CodecsCsvOptions_Example()
{
string csvFileName = Path.Combine(LEAD_VARS.ImagesDir, "sample.csv");
string pngFileName = Path.Combine(LEAD_VARS.ImagesDir, "sample-csv.png");
using (RasterCodecs codecs = new RasterCodecs())
{
// Change some CSV options
CodecsCsvLoadOptions csvOptions = codecs.Options.Csv.Load; // CodecsCsvOptions reference
csvOptions.HeaderFontItalic = true;
csvOptions.BodyFontName = "Arial";
csvOptions.TableBorderColor = RasterColor.FromKnownColor(RasterKnownColor.Red);
// Load the first page from a CSV file
using (RasterImage image = codecs.Load(csvFileName, 1))
{
// Save it as PNG
codecs.Save(image, pngFileName, RasterImageFormat.Png, 0);
}
}
}
/// <!--CodecsCsvOptions-->
/// <!--CodecsPdfLoadOptions.EnhanceThinLines-->
public void CodecsEnhanceThinLinesOption_Example()
{
/* The following example shows you how to disable the thin line enhancement property */
RasterCodecs codecs = new RasterCodecs();
codecs.Options.Pdf.Load.EnhanceThinLines = false;
string inputFile = @"MyFile.PDF";
RasterImage rawImage = codecs.Load(inputFile, 24, CodecsLoadByteOrder.RgbOrGray, 1, -1);
}
/// <!--CodecsPdfLoadOptions.EnhanceThinLines-->
/// <!--CodecsTxtLoadOptions.DefaultEncoding-->
// This C# example shows you how to load a file called "Ansi.txt" that contains ANSI text.
public void CodecsTxtLoadOptionsDefaultEncoding_Example()
{
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFile = @"Ansi.txt";
using (CodecsImageInfo info = codecs.GetInformation(srcFile, false))
{
if (!info.HasBom)
codecs.Options.Txt.Load.DefaultEncoding = CodecsTxtEncoding.Ansi; /* Load as Ansi. Here you can bring up a message box asking the user to select the encoding */
codecs.Options.Load.AllPages = true;
using (RasterImage image = codecs.Load(srcFile))
{
codecs.Save(image, srcFile + ".tif", RasterImageFormat.TifLzw, 0);
}
}
}
}
/// <!--CodecsTxtLoadOptions.DefaultEncoding-->
/// <!--CodecsLoadOptions.Decrypt-->
public void CodecsLoadOptionsDecrypt_Example()
{
/* This sample loads Encrypted.docx, which is encrypted with the password 'MyPassword' */
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFile = @"Encrypted.docx";
codecs.Options.Load.Decrypt.Password = "MyPassword";
codecs.Options.Load.AllPages = true;
using (RasterImage image = codecs.Load(srcFile))
{
codecs.Save(image, srcFile + ".tif", RasterImageFormat.TifLzw, 0);
}
}
}
/// <!--CodecsLoadOptions.Decrypt-->
///<!--CodecsHeicOptions-->
public void CodecsHeicOptions_Example()
{
RasterCodecs codecs = new RasterCodecs();
string fileName = "C:\temp\test.heic";
using (RasterImage image = codecs.Load(fileName))
{
/* Save a HEIC file using qFactor of 30 and no stamp */
string outFile30 = @"C:\temp\out30-NoStamp.heic";
codecs.Options.Heic.Load.Multithreaded = false; // CodecsHeicLoadOptions reference
codecs.Options.Heic.Save.QualityFactor = 30; // CodecsHeicOptions & CodecsHeicSaveOptions reference
codecs.Save(image, outFile30, RasterImageFormat.Heic, 0);
/* Save a HEIC file using qFactor of 40 and a stamp */
string outFile40 = @"C:\temp\out40-Stamp.heic";
codecs.Options.Heic.Save.QualityFactor = 40;
codecs.Options.Heic.Save.SaveWithStamp = true;
codecs.Options.Heic.Save.StampWidth = 320;
codecs.Options.Heic.Save.StampHeight = codecs.Options.Heic.Save.StampWidth * image.Height / image.Width;
codecs.Options.Heic.Save.StampBitsPerPixel = 24;
codecs.Save(image, outFile40, RasterImageFormat.Heic, 0);
}
// Clean up
codecs.Dispose();
}
///<!--CodecsHeicOptions-->
///<!--CodecsPowerPointOptions-->
public static void CodecsPowerPointOptions_Example()
{
RasterCodecs codecs = new RasterCodecs();
string filename = @"C:\temp\test.ppt";
string savefilename = @"C:\temp\test-save.ppt";
/* Specify PowerPointLoadOptions */
CodecsPowerPointLoadOptions loadOptions = codecs.Options.PowerPoint.Load;
loadOptions.ShowHiddenSlides = false;
/* Load and Save PowerPoint */
using (RasterImage image = codecs.Load(filename, 1))
{
codecs.Save(image, savefilename, RasterImageFormat.Ppt, 24);
}
// Clean up
codecs.Dispose();
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
}