LEADTOOLS Image File Support (Leadtools.Codecs assembly)
LEAD Technologies, Inc

SaveStamp(RasterImage,String,Int32,Int32,Int32,CodecsSavePageMode) Method

Example 





The Leadtools.RasterImage object that contain the thumbnail (stamp) image.
A System.String containing the name of an existing image file.
1-based index of the first page in image to save.
1-based index of the last page in image to save. Pass -1 to save from firstPage to the last page in image.
1-based index of the first output page. If the output file already exists, then this parameter lets you control which pages to overwrite and/or where to append the new pages.

Determines how to handle the page when saving to multipage formats. This can be one of the following:

Value Meaning
CodecsSavePageMode.Append Append the new page(s) to the end of the file. If the file does not exist, this option will create the file and add the pages to it. firstSavePageNumber is not used.
CodecsSavePageMode.Insert Insert the new page(s) at the index specified by firstSavePageNumber.
CodecsSavePageMode.Replace Replace the page(s) starting at the index specified by firstSavePageNumber.
CodecsSavePageMode.Overwrite Overwrite the page(s) starting at the index specified by firstSavePageNumber.
Saves a stamp in an existing file with specific options. .NET support Silverlight support
Syntax
public void SaveStamp( 
   RasterImage image,
   string fileName,
   int firstPage,
   int lastPage,
   int firstSavePageNumber,
   CodecsSavePageMode pageMode
)
'Declaration
 
Public Overloads Sub SaveStamp( _
   ByVal image As RasterImage, _
   ByVal fileName As String, _
   ByVal firstPage As Integer, _
   ByVal lastPage As Integer, _
   ByVal firstSavePageNumber As Integer, _
   ByVal pageMode As CodecsSavePageMode _
) 
'Usage
 
Dim instance As RasterCodecs
Dim image As RasterImage
Dim fileName As String
Dim firstPage As Integer
Dim lastPage As Integer
Dim firstSavePageNumber As Integer
Dim pageMode As CodecsSavePageMode
 
instance.SaveStamp(image, fileName, firstPage, lastPage, firstSavePageNumber, pageMode)
public void SaveStamp( 
   RasterImage image,
   string fileName,
   int firstPage,
   int lastPage,
   int firstSavePageNumber,
   CodecsSavePageMode pageMode
)
 function Leadtools.Codecs.RasterCodecs.SaveStamp(RasterImage,String,Int32,Int32,Int32,CodecsSavePageMode)( 
   image ,
   fileName ,
   firstPage ,
   lastPage ,
   firstSavePageNumber ,
   pageMode 
)
public:
void SaveStamp( 
   RasterImage^ image,
   String^ fileName,
   int firstPage,
   int lastPage,
   int firstSavePageNumber,
   CodecsSavePageMode pageMode
) 

Parameters

image
The Leadtools.RasterImage object that contain the thumbnail (stamp) image.
fileName
A System.String containing the name of an existing image file.
firstPage
1-based index of the first page in image to save.
lastPage
1-based index of the last page in image to save. Pass -1 to save from firstPage to the last page in image.
firstSavePageNumber
1-based index of the first output page. If the output file already exists, then this parameter lets you control which pages to overwrite and/or where to append the new pages.
pageMode

Determines how to handle the page when saving to multipage formats. This can be one of the following:

Value Meaning
CodecsSavePageMode.Append Append the new page(s) to the end of the file. If the file does not exist, this option will create the file and add the pages to it. firstSavePageNumber is not used.
CodecsSavePageMode.Insert Insert the new page(s) at the index specified by firstSavePageNumber.
CodecsSavePageMode.Replace Replace the page(s) starting at the index specified by firstSavePageNumber.
CodecsSavePageMode.Overwrite Overwrite the page(s) starting at the index specified by firstSavePageNumber.
Remarks

The stamp will be written to last page in the file. If that page already has a stamp, it will be overwritten; If the file to which the stamp is being written does not exist, an exception will occur.

This method works for regular JPEG files and Exif JPEG files. For regular JPEG files, the stamp saved is always uncompressed and can be 8 or 24 bits per pixel. The stamp can be any width and height, but the stamp data and stamp header must completely fit in an APP0 marker. Therefore, the size must be less than 64k bytes (0xFFFF).

For Exif JPEG files, the stamps can be uncompressed or JPEG compressed and must be 24 bits per pixel. Exif JPEG stamps are supposed to be 160 x 120. However, LEADTOOLS can save and read Exif JPEG stamps of other dimensions. The stamp, along with other information such as the TIFF_HEADER, 0th IFD, 1st IFD, etc. must completely fit in an APP1 marker. Therefore, the size must be less than 64k bytes (0xFFFF).

NOTE: At this time, there are no multipage formats that support stamps.

Example
 
Public Sub StampExample()
      Dim codecs As RasterCodecs = New RasterCodecs()

      Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
      Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_WithCustomStamp.cmp")
      Dim stampFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Stamp.bmp")

      ' Load the source file name
      Dim image As RasterImage = codecs.Load(srcFileName)

      ' Save as the destination image
      codecs.Save(image, destFileName, RasterImageFormat.Cmp, 24)

      ' Resize the image to fit into 128 by 128 pixels keeping the aspect ratio
      Dim rc As LeadRect = New LeadRect(0, 0, 128, 128)
      rc = RasterImage.CalculatePaintModeRectangle(image.ImageWidth, image.ImageHeight, rc, RasterPaintSizeMode.FitAlways, RasterPaintAlignMode.Near, RasterPaintAlignMode.Near)

      Dim command As SizeCommand = New SizeCommand()
      command.Width = rc.Width
      command.Height = rc.Height
      command.Flags = RasterSizeFlags.None
      command.Run(image)

      ' Add the word "Stamp" on the image at the middle
      Dim message As String = "Stamp"

      Using rg As RasterGraphics = RasterImagePainter.CreateGraphics(image)
         Using sf As New System.Drawing.StringFormat()
            sf.Alignment = System.Drawing.StringAlignment.Center
            sf.LineAlignment = System.Drawing.StringAlignment.Center
            Using f As New System.Drawing.Font("Arial", 20, System.Drawing.FontStyle.Bold)
               Dim rcDraw As New System.Drawing.Rectangle(rc.X, rc.Y, rc.Width, rc.Height)
               rg.Graphics.DrawString(message, f, System.Drawing.Brushes.Yellow, rcDraw, sf)
            End Using
         End Using
      End Using

      ' Now set this image as the stamp for this file
      codecs.SaveStamp(image, destFileName, 1, 1, 1, CodecsSavePageMode.Overwrite)
      image.Dispose()

      ' Load the stamp from the file and save it into another file
      image = codecs.ReadStamp(destFileName, 1)
      codecs.Save(image, stampFileName, RasterImageFormat.Bmp, 24)
      image.Dispose()

      ' Clean up
      codecs.Dispose()
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void StampExample()
     {
         RasterCodecs codecs = new RasterCodecs();

         string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
         string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_WithCustomStamp.cmp");
         string stampFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Stamp.bmp");

         // Load the source file name
         RasterImage image = codecs.Load(srcFileName);

         // Save as the destination image
         codecs.Save(image, destFileName, RasterImageFormat.Cmp, 24);

         // Resize the image to fit into 128 by 128 pixels keeping the aspect ratio
         LeadRect rc = new LeadRect(0, 0, 128, 128);
         rc = RasterImage.CalculatePaintModeRectangle(
            image.ImageWidth,
            image.ImageHeight,
            rc,
            RasterPaintSizeMode.FitAlways,
            RasterPaintAlignMode.Near,
            RasterPaintAlignMode.Near);

         SizeCommand command = new SizeCommand();
         command.Width = rc.Width;
         command.Height = rc.Height;
         command.Flags = RasterSizeFlags.None;
         command.Run(image);

         // Add the word "Stamp" on the image at the middle
         string message = "Stamp";

         using (Leadtools.Drawing.RasterGraphics rg = Leadtools.Drawing.RasterImagePainter.CreateGraphics(image))
         {
             using (System.Drawing.StringFormat sf = new System.Drawing.StringFormat())
             {
                 sf.Alignment = System.Drawing.StringAlignment.Center;
                 sf.LineAlignment = System.Drawing.StringAlignment.Center;
                 using (System.Drawing.Font f = new System.Drawing.Font("Arial", 20, System.Drawing.FontStyle.Bold))
                 {
                     System.Drawing.Rectangle rcDraw = new System.Drawing.Rectangle(rc.X, rc.Y, rc.Width, rc.Height);
                     rg.Graphics.DrawString(message, f, System.Drawing.Brushes.Yellow, rcDraw, sf);
                 }
             }
         }

         // Now set this image as the stamp for this file
         codecs.SaveStamp(image, destFileName, 1, 1, 1, CodecsSavePageMode.Overwrite);
         image.Dispose();

         // Load the stamp from the file and save it into another file
         image = codecs.ReadStamp(destFileName, 1);
         codecs.Save(image, stampFileName, RasterImageFormat.Bmp, 24);
         image.Dispose();

         // Clean up
         codecs.Dispose();
     }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
RasterCodecsExamples.prototype.StampExample = function ( ) 
{
   Tools.SetLicense ( ) ;
   with (Leadtools) {
      with (Leadtools.Codecs) {
         var codecs = new RasterCodecs();

         var srcFileName = "Assets\\Image1.cmp";
         var destFileName = "Image1_WithCustomStamp.cmp";
         var stampFileName = "Image1_Stamp.bmp";
         var image;
         var saveFile;
         // Load the source file name
         return Tools.AppInstallFolder().getFileAsync(srcFileName).then(function (loadFile) {
            return codecs.loadAsync(LeadStreamFactory.create(loadFile))
         })
         .then(function (img) {
            image = img;

            // Save as the destination image
            return Tools.AppLocalFolder().createFileAsync(destFileName)
         })
         .then(function (saveFileX) {
            saveFile = saveFileX;
            return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.cmp, 24)
         })
         .then(function () {

            // Resize the image to fit into 128 by 128 pixels keeping the aspect ratio
            var rc = LeadRectHelper.create(0, 0, 128, 128);
            rc = RasterImage.calculatePaintModeRectangle(
               image.imageWidth,
               image.imageHeight,
               rc,
               RasterPaintSizeMode.fitAlways,
               RasterPaintAlignMode.near,
               RasterPaintAlignMode.near);

            var command = new Leadtools.ImageProcessing.SizeCommand();
            command.width = rc.width;
            command.height = rc.height;
            command.flags = RasterSizeFlags.none;
            command.run(image);

            // Now set this image as the stamp for this file
            return Tools.AppLocalFolder().getFileAsync(destFileName)
         })
         .then(function (saveFile) {
            return codecs.saveStampAsync(image, LeadStreamFactory.create(saveFile), 1, 1, 1, CodecsSavePageMode.overwrite)
         })
         .then(function () {
            image.close();

            // Load the stamp from the file and save it into another file
            return codecs.readStampAsync(LeadStreamFactory.create(saveFile), 1)
         })
         .then(function (img) {
            image = img;
            return Tools.AppLocalFolder().createFileAsync(stampFileName)
         })
         .then(function (saveFile) {
            return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.bmp, 24)
         })
         .then(function () {
            image.close();

            // Clean up
            codecs.close();
         });
      }
   }
}
[TestMethod]
public async Task StampExample()
{
   RasterCodecs codecs = new RasterCodecs();
   string srcFileName = @"Assets\Image1.cmp";
   string destFileName = @"Image1_WithCustomStamp.cmp";
   string stampFileName = @"Image1_Stamp.bmp";

   // Load the source file name
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile));

   // Save as the destination image
   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Cmp, 24);

   // Resize the image to fit into 128 by 128 pixels keeping the aspect ratio
   LeadRect rc = LeadRectHelper.Create(0, 0, 128, 128);
   rc = RasterImage.CalculatePaintModeRectangle(
      image.ImageWidth,
      image.ImageHeight,
      rc,
      RasterPaintSizeMode.FitAlways,
      RasterPaintAlignMode.Near,
      RasterPaintAlignMode.Near);

   SizeCommand command = new SizeCommand();
   command.Width = rc.Width;
   command.Height = rc.Height;
   command.Flags = RasterSizeFlags.None;
   command.Run(image);

   // Now set this image as the stamp for this file
   saveFile = await Tools.AppLocalFolder.GetFileAsync(destFileName);
   await codecs.SaveStampAsync(image, LeadStreamFactory.Create(saveFile), 1, 1, 1, CodecsSavePageMode.Overwrite);
   image.Dispose();

   // Load the stamp from the file and save it into another file
   image = await codecs.ReadStampAsync(LeadStreamFactory.Create(saveFile), 1);
   saveFile = await Tools.AppLocalFolder.CreateFileAsync(stampFileName);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 24);
   image.Dispose();

   // Clean up
   codecs.Dispose();
}
private void StampExample(RasterImage image, Stream outStreamFileWithStamp, Stream outStreamStampOnly)
{
   RasterCodecs codecs = new RasterCodecs();
   // Save as the destination image
   codecs.Save(image, outStreamFileWithStamp, RasterImageFormat.Cmp, 24);

   // Resize the image to fit into 128 by 128 pixels keeping the aspect ratio
   LeadRect rc = new LeadRect(0, 0, 128, 128);
   rc = RasterImage.CalculatePaintModeRectangle(
      image.ImageWidth,
      image.ImageHeight,
      rc,
      RasterPaintSizeMode.FitAlways,
      RasterPaintAlignMode.Near,
      RasterPaintAlignMode.Near);

   SizeCommand command = new SizeCommand();
   command.Width = rc.Width;
   command.Height = rc.Height;
   command.Flags = RasterSizeFlags.None;
   command.Run(image);

   // Add the word "Stamp" on the image at the middle
   string message = "Stamp";

   // To add "Stamp", we will create a WriteableBitmap from the RasterImage
   // then add a canvas and a text block on top of it

   // Create a writeable bitmap from the image source
   WriteableBitmap bitmap = new WriteableBitmap(image.ImageWidth, image.ImageHeight);

   // The white background canvas
   Canvas canvas = new Canvas();
   canvas.Width = image.ImageWidth;
   canvas.Height = image.ImageHeight;

   // Get the RasterImage as an ImageSource
   ImageSource source = RasterImageConverter.ConvertToSource(image, ConvertToSourceOptions.UseSetSource);

   // The image for the ImageSource
   Image img = new Image();
   img.Source = source;

   // The black text block
   TextBlock textBlock = new TextBlock();
   textBlock.FontFamily = new FontFamily("Arial");
   textBlock.FontSize = 20;
   textBlock.Text = message;
   textBlock.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));

   // Add the image source and the text block
   canvas.Children.Add(img);
   canvas.Children.Add(textBlock);

   // Center the text
   double left = (canvas.Width - textBlock.ActualWidth) / 2;
   double top = (canvas.Height - textBlock.ActualHeight) / 2;
   textBlock.SetValue(Canvas.LeftProperty, left);
   textBlock.SetValue(Canvas.TopProperty, top);

   // Render
   bitmap.Render(canvas, null);
   bitmap.Invalidate();

   image.Dispose();

   // Re-create the RasterImage from the WriteableBitmap
   image = RasterImageConverter.ConvertFromSource(bitmap, ConvertFromSourceOptions.None);

   // stamp has to be 24-bit, so convert it back
   ColorResolutionCommand cmd = new ColorResolutionCommand();
   cmd.BitsPerPixel = 24;
   cmd.Run(image);

   // This image contains the stamp, save it to the output stream
   codecs.SaveStamp(image, outStreamFileWithStamp);
   image.Dispose();

   // Load the stamp from the file and save it to the other output stream
   image = codecs.ReadStamp(outStreamFileWithStamp, 1);
   codecs.Save(image, outStreamStampOnly, RasterImageFormat.Bmp, 24);
   image.Dispose();
}
Private Sub StampExample(ByVal image As RasterImage, ByVal outStreamFileWithStamp As Stream, ByVal outStreamStampOnly As Stream)
   Dim codecs As RasterCodecs = New RasterCodecs()
   ' Save as the destination image
   codecs.Save(image, outStreamFileWithStamp, RasterImageFormat.Cmp, 24)

   ' Resize the image to fit into 128 by 128 pixels keeping the aspect ratio
   Dim rc As LeadRect = New LeadRect(0, 0, 128, 128)
   rc = RasterImage.CalculatePaintModeRectangle(image.ImageWidth, image.ImageHeight, rc, RasterPaintSizeMode.FitAlways, RasterPaintAlignMode.Near, RasterPaintAlignMode.Near)

   Dim command As SizeCommand = New SizeCommand()
   command.Width = rc.Width
   command.Height = rc.Height
   command.Flags = RasterSizeFlags.None
   command.Run(image)

   ' Add the word "Stamp" on the image at the middle
   Dim message As String = "Stamp"

   ' To add "Stamp", we will create a WriteableBitmap from the RasterImage
   ' then add a canvas and a text block on top of it

   ' Create a writeable bitmap from the image source
   Dim bitmap As WriteableBitmap = New WriteableBitmap(image.ImageWidth, image.ImageHeight)

   ' The white background canvas
   Dim canvas As Canvas = New Canvas()
   canvas.Width = image.ImageWidth
   canvas.Height = image.ImageHeight

   ' Get the RasterImage as an ImageSource
   Dim source As ImageSource = RasterImageConverter.ConvertToSource(image, ConvertToSourceOptions.UseSetSource)

   ' The image for the ImageSource
   Dim img As Image = New Image()
   img.Source = source

   ' The black text block
   Dim textBlock As TextBlock = New TextBlock()
   textBlock.FontFamily = New FontFamily("Arial")
   textBlock.FontSize = 20
   textBlock.Text = message
   textBlock.Foreground = New SolidColorBrush(Color.FromArgb(255, 0, 0, 0))

   ' Add the image source and the text block
   canvas.Children.Add(img)
   canvas.Children.Add(textBlock)

   ' Center the text
   Dim left As Double = (canvas.Width - textBlock.ActualWidth) / 2
   Dim top As Double = (canvas.Height - textBlock.ActualHeight) / 2
   textBlock.SetValue(Canvas.LeftProperty, left)
   textBlock.SetValue(Canvas.TopProperty, top)

   ' Render
   bitmap.Render(canvas, Nothing)
   bitmap.Invalidate()

   image.Dispose()

   ' Re-create the RasterImage from the WriteableBitmap
   image = RasterImageConverter.ConvertFromSource(bitmap, ConvertFromSourceOptions.None)

   ' stamp has to be 24-bit, so convert it back
   Dim cmd As ColorResolutionCommand = New ColorResolutionCommand()
   cmd.BitsPerPixel = 24
   cmd.Run(image)

   ' This image contains the stamp, save it to the output stream
   codecs.SaveStamp(image, outStreamFileWithStamp)
   image.Dispose()

   ' Load the stamp from the file and save it to the other output stream
   image = codecs.ReadStamp(outStreamFileWithStamp, 1)
   codecs.Save(image, outStreamStampOnly, RasterImageFormat.Bmp, 24)
   image.Dispose()
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

RasterCodecs Class
RasterCodecs Members
Overload List
Implementing Extended FlashPix Support

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.