Loading and Saving Images

Show in webframe

LEADTOOLS provides many options when loading and saving image files. Nevertheless, the code can be as simple as the following, which loads a LEAD compressed file and saves it as a Windows BMP file:

Visual Basic

' Create a new instance of the RasterCodecs classDim codecs As New Leadtools.Codecs.RasterCodecs()
             ' Load an image at its own bits per pixel
             Dim image As Leadtools.RasterImage = codecs.Load( _
                "c:\Test.cmp", _
                0, _
                Leadtools.Codecs.CodecsLoadByteOrder.Bgr, _
                1, _
                1)
             ' Save the image as a 24-bit Windows BMP file
             codecs.Save( _
                image, _
                "C:\Test.bmp", _
                Leadtools.RasterImageFormat.Bmp, _
                24, _
                1, _
                1, _
                1, _
                Leadtools.Codecs.CodecsSavePageMode.Overwrite)
             ' Dispose of the objects we created
             image.Dispose()
             codecs.Dispose()
             

C#


             // Create a new instance of the RasterCodecs class
             Leadtools.Codecs.RasterCodecs codecs = new Leadtools.Codecs.RasterCodecs();
             // Load an image at its own bits per pixel
             Leadtools.RasterImage image = codecs.Load(
                @"c:\Test.cmp",
                0,
                Leadtools.Codecs.CodecsLoadByteOrder.Bgr,
                1,
                1);
             // Save the image as a 24-bit Windows BMP file
             codecs.Save(
                image,
                @"C:\Test.bmp",
                Leadtools.RasterImageFormat.Bmp,
                24,
                1,
                1,
                1,
                Leadtools.Codecs.CodecsSavePageMode.Overwrite);
             // Dispose of the objects we created
             image.Dispose();
             codecs.Dispose();
             

J#


             // Create a new instance of the RasterCodecs class
             Leadtools.Codecs.RasterCodecs codecs = new Leadtools.Codecs.RasterCodecs();
             // Load an image at its own bits per pixel
             Leadtools.RasterImage image = codecs.Load("c:\\PSPMainWindow.JPG", 0, Leadtools.Codecs.CodecsLoadByteOrder.Bgr, 1, 1);
             Toolkit.getDefaultToolkit().getImage(getClass().getResource("mario.gif"));
             // Save the image as a 24-bit Windows BMP file
             codecs.Save(image,"c:\\Test.bmp" ,Leadtools.RasterImageFormat.Bmp,24,1,1,1,Leadtools.Codecs.CodecsSavePageMode.Overwrite);
             // Dispose of the objects we created
             image.Dispose();
             codecs.Dispose();
             

LEADTOOLS also supports super compressed images. Only 1-bit and 24-bit images can be kept super compressed in memory. For more information, refer to Super Compressed Images. You can load an image at its own color resolution, manipulate it, and display it, regardless of current video mode. You can also convert the image to a specific color resolution (bits per pixel) as you load or save it. If you want to convert while loading or saving, refer to Conversion Considerations.

Whether an image file is on disk or in memory, you can get information about the image before loading it. You can also handle file-format-specific information, such as the page number or physical resolution. The following topics provide more information about specific file formats:

You can control the execution of a load procedure as you supply transmitted image data. Refer to the RasterCodecs.StartFeedLoad, RasterCodecs.FeedLoad, and RasterCodecs.StopFeedLoad methods.

The FastFileInfo allows you to determine if a file will be rejected if the signature does not match one of the known file format signatures. As a result the file format dlls will not be loaded or called unnecessarily.

The Load method lets you resize the image to new dimensions as it is loaded by setting the desired width and height. The MrcSegmenter.LoadImage method loads an MRC image file into an image (the file can be in any supported image file format.)

Both RAW headerless image data and raw FAX load and save are supported using the Raw codec (Leadtools.Codecss.Raw.dll).

To save an image to a file in a memory buffer, Save uses an optional callback method to allow additional processing of the saved material.

Regions are saved automatically inside TIFF files. For more information, refer to Saving a Region. Note, however, that the ability to save a region inside a TIFF file must be unlocked. This requires a Document Imaging or Medical Imaging toolkit.

Window-leveling applied with the WindowLevelCommand class can be saved to TIF or DICOM formats without changing the image data. For more information, refer to Saving Window-Leveled Images.

LEADTOOLS supports loading TIFF CMYK files without converting the data to BGR. This is done by loading each CMYK plane as a separate page in an Image using LoadCmykPlanes. To save the planes as TIFF CMYK, call SaveCmykPlanes. For more information, refer to Handling CMYK Files as Separate Images.

Saving Colored Images as Bitonal

LEADTOOLS has the capability of automatically saving a colored image (an image that has more than 1 bit/pixel) as bitonal (an image that has 1 bit/pixel). This is accomplished by passing "1" as the bitsPerPixel parameter of the various RasterCodecs.Save methods. Whenever you reduce an image's color resolution to 8 bits per pixel or less, a dithering method comes into play. One alternative is to use a nearest-color match (no dithering), which means that the color of each pixel is changed to the palette color that most closely matches it. If the original image contains subtle color details, the result of a nearest-color match may have large blotches of color that are not very pleasing.

Dithering methods create the appearance of more subtle shades by mixing in pixels of different colors. This is similar to the way newspaper pictures produce the appearance of shades of gray, even though the only actual colors are black and white.

Starting with v17, LEADTOOLS will not use dithering when saving a colored image as bitonal (1-bit/pixel) by default. This will guarantee that the final bitonal image contains the clearest representation of the original text in the image and provides the best input for document based recognition methods such as OCR and Barcode.

To enable dithering, you must set the dithering method inside the Leadtools.RasterImage to the required value and then instruct Leadtools.Codecs.RasterCodecs to use this value when saving the image. The following example will load a 24 bpp image and save it as bitonal with and without dithering.

Visual Basic


             Private Shared Sub Test(ByVal codecs As RasterCodecs, ByVal coloredImageFileName As String)
                ' RasterCodecs.Options.Save.UseImageDitheringMethod is false by default
            
                Dim image As RasterImage = codecs.Load(coloredImageFileName)
                ' Save it with no dithering options
                codecs.Save(image, "C:\NotDithered.tif", RasterImageFormat.CcittGroup4, 1)
            
                ' Change the image dithering method to FloydStein
                image.DitheringMethod = RasterDitheringMethod.FloydStein
                ' Use the image dithering method when saving
                codecs.Options.Save.UseImageDitheringMethod = True
                ' Save it again
                codecs.Save(image, "C:\Dithered.tif", RasterImageFormat.CcittGroup4, 1)
            
                image.Dispose()
             End Sub
             

C#


             private static void Test(RasterCodecs codecs, string coloredImageFileName)
             {
                // RasterCodecs.Options.Save.UseImageDitheringMethod is false by default
            
                RasterImage image = codecs.Load(coloredImageFileName);
                // Save it with no dithering options
                codecs.Save(image, @"C:\NotDithered.tif", RasterImageFormat.CcittGroup4, 1);
            
                // Change the image dithering method to FloydStein
                image.DitheringMethod = RasterDitheringMethod.FloydStein;
                // Use the image dithering method when saving
                codecs.Options.Save.UseImageDitheringMethod = true;
                // Save it again
                codecs.Save(image, @"C:\Dithered.tif", RasterImageFormat.CcittGroup4, 1);
            
                image.Dispose();
             }
             

LEADTOOLS Windows Presentation Foundation(WPF) and Silverlight Support

LEADTOOLS provides extensive support for WPF and Silverlight. To learn how to display LEADTOOLS images in WPF/Silverlight, as well as convert them to and from WPF/Silverlight images refer to RasterImage and Wpf/Silverlight

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.