←Select platform

ConvertToSource(RasterImage,ConvertToSourceOptions,double) Method

Summary

Converts a LEADTOOLS for .NET Leadtools.RasterImage object to WPF/Silverlight System.Windows.Media.ImageSource.

Syntax

C#
VB
C++
public static ImageSource ConvertToSource( 
   RasterImage image, 
   ConvertToSourceOptions options, 
   double resizeRatio 
) 
  
Public Shared Function ConvertToSource( _ 
   ByVal image As Leadtools.RasterImage, _ 
   ByVal options As Leadtools.Windows.Media.ConvertToSourceOptions, _ 
   ByVal resizeRatio As Double 
) As ImageSource 
public: 
static ImageSource^ ConvertToSource(  
   Leadtools.RasterImage^ image, 
   Leadtools.Windows.Media.ConvertToSourceOptions options, 
   double resizeRatio 
)  

Parameters

image
The Leadtools.RasterImage object containing the image data to convert.

options
Options to control the conversion operation. The following values can be ORed together:

Value Meaning
ConvertToSourceOptions.None No options
ConvertToSourceOptions.KeepAlphaValues If the source Leadtools.RasterImage is 32 or 64-bits/pixel, then use the alpha values from this alpha if the result of the conversion is also a 32 or 64-bits/pixel System.Windows.Media.ImageSource. If this flagged is not specified, the result System.Windows.Media.ImageSource will always have an alpha value of all 1's (255 for 32-bit images or 65535 for 64-bit images) regardless of the alpha values in the source Leadtools.RasterImage. This can be useful when converting some 32-bit images that may have 0 for the alpha (such as BMP files) which translates into an System.Windows.Media.ImageSource that is completely transparent. If this flag is specified, then the source image alpha values will be used as is.
ConvertToSourceOptions.KeepViewPerspective Keep the original Leadtools.RasterImage view perspective. The Leadtools.RasterImage objects supports having an image data orientation other than the normal top-left through the RasterImage.ViewPerspective property. If the source Leadtools.RasterImage has a view-perspective other than RasterViewPerspective.TopLeft and this flag is specified, then the result System.Windows.Media.ImageSource might be flipped, reversed or rotated. For example, if the source view-perspective is RasterViewPerspective.BottomLeft and this flag is specified in the conversion options, then the result System.Windows.Media.ImageSource will be flipped. If this flag is not specified, then the conversion might flip, reverse or rotate the image data as needed to provide correct (top-left) System.Windows.Media.ImageSource.
ConvertToSourceOptions.IgnoreLowHighBitsOnGrayImages Do not use the gray scale bit window values when converting 12 or 16-bit grayscale Leadtools.RasterImage objects. The Leadtools.RasterImage object contains the RasterImage.LowBit and RasterImage.HighBit values that can be set to provide a Window into the image data. This is mostly used by medical applications during a window-leveling operations. If this flag is specified, the RasterImage.LowBit and RasterImage.HighBit values are ignored and all of the image data is used when converting the Leadtools.RasterImage to System.Windows.Media.ImageSource. If this flag is not specified, then only the image data between RasterImage.LowBit and RasterImage.HighBit is used.
ConvertToSourceOptions.UseSetSource Note: This option is only used by the LEADTOOLS for Silverlight toolkit. The WPF version of this flag is ignored and have no effect on the conversion operation. If this flag is not specified, then BitmapSource.SetSource is not used and the toolkit will directly convert the Leadtools.RasterImage to a compatible System.Windows.Media.ImageSource. Specifying this flag in the LEADTOOLS for Silverlight toolkit may increase the performance of the RasterImageConverter.ConvertToSource method especially when converting 1 bits/pixel Leadtools.RasterImage objects.

resizeRatio
Specifies the ratio for resizing the image before converting to WritableBitmap. This is useful when viewing large images to reduce memory usage.

Return Value

An System.Windows.Media.ImageSource object representing the converted image.

Remarks

Microsoft Windows Presentation Foundation (WPF)

LEADTOOLS for .NET and Windows Presentation Foundation (WPF) support different pixel formats. These pixel formats may not be identical in some cases. The conversion process takes care of converting scanlines to a suitable format that best matches the source data.

The ConvertToSource method supports converting from any Leadtools.RasterImage regardless of the combination of the bits/pixel, compression, color order, grayscale properties of the source image. The ConvertToSource method will always returns a valid WPF System.Windows.Media.ImageSource by allocating temporary memory for the data and performing the necessary conversion on it.

In some cases, the Leadtools.RasterImage data is compatible with WPF and no conversion takes place. Converting a compatible Leadtools.RasterImage to WPF System.Windows.Media.ImageSource is faster and takes less resources than converting a non-compatible image. Please note that currently WPF does not allow direct access to the raw data pointers where the image data is stored. Therefore, there is always at least one copy operation regardless if the source data can be used directly by a WPF image (For example, through the use of the System.Windows.Media.Imaging.BitmapSource.Create method).

For a Leadtools.RasterImage to be compatible with WPF, it must have the following properties:

The following table lists the source Leadtools.RasterImage bits/pixel and source data properties and whether each combination is compatible with WPF.

Source Bits/Pixel More Source Data Compatible Result PixelFormat
1 Yes System.Windows.Media.PixelFormats.BlackWhite
1 Yes System.Windows.Media.PixelFormats.Indexed1
2 Yes System.Windows.Media.PixelFormats.Indexed2
3 No System.Windows.Media.PixelFormats.Indexed4
4 Yes System.Windows.Media.PixelFormats.Indexed4
5 No System.Windows.Media.PixelFormats.Indexed8
6 No System.Windows.Media.PixelFormats.Indexed8
7 No System.Windows.Media.PixelFormats.Indexed8
8 Yes System.Windows.Media.PixelFormats.Gray8
8 Yes System.Windows.Media.PixelFormats.Gray8
12 No System.Windows.Media.PixelFormats.Gray16
16 Yes System.Windows.Media.PixelFormats.Gray16
16 No System.Windows.Media.PixelFormats.Bgra32
16 Yes System.Windows.Media.PixelFormats.Bgr555
24 Yes System.Windows.Media.PixelFormats.Bgr24
24 Yes System.Windows.Media.PixelFormats.Rgb24
32 Yes System.Windows.Media.PixelFormats.Bgra32
32 No System.Windows.Media.PixelFormats.Bgra32
48 No System.Windows.Media.PixelFormats.Rgb48
48 Yes System.Windows.Media.PixelFormats.Rgb48
64 No System.Windows.Media.PixelFormats.Rgba64
64 Yes System.Windows.Media.PixelFormats.Rgba64

Notes on gray scale images

Both LEADTOOLS and Windows Presentation Foundation support 16 bit grayscale image data. However LEADTOOLS supports extra features such as low/high bit values and color lookup tables. For a LEADTOOLS 16-bit grayscale Leadtools.RasterImage to be compatible with a WPF image with System.Windows.Media.PixelFormats.Gray16, the following conditions must be met:

Notes on alpha channel

When converting images that support an alpha channel (32 or 64 bits/pixel images), the ConvertToSource method supports masking out the alpha channel bits to all 1's through or using the source image alpha channel bits through the use of the ConvertToSourceOptions.KeepAlphaValues flag. Masking out the alpha channel bits requires the source data image to be processed and as a result, the image will not be compatible with WPF.

Microsoft Silverlight

The Microsoft Silverlight platform supports creating images as 32-bits per pixel only. So only a Leadtools.RasterImage with the following properties is considered Silverlight-compatible:

RasterImage.IsConventionalMemory is true, RasterImage.BitsPerPixel is 32 and RasterImage.Order is RasterByteOrder.Bgr.

Any Leadtools.RasterImage with any other combination with image color and data format will be converted in scanline by scanline bases.

You can also use the ConvertToSourceOptions.UseSetSource options to speed up the conversion process for incompatible image data as described above.

For more information refer to RasterImage and WPF/Silverlight.

Example

This example will use LEADTOOLS to load a Leadtools.RasterImage, convert to a WPF/Silverlight System.Windows.Media.ImageSource and set it in a System.Windows.Controls.Image control.

C#
VB
Silverlight C#
Silverlight VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.Windows.Media; 
 
private void ConvertToSourceExample(System.Windows.Controls.Image imageControl) 
{ 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
 
 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      // Load the image 
      using (RasterImage rasterImage = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.Bgr, 1, 1)) 
      { 
         // Convert it to WPF/Silverlight ImageSource 
         System.Windows.Media.ImageSource source = RasterImageConverter.ConvertToSource(rasterImage, ConvertToSourceOptions.None); 
 
         // Set it in the Image control 
         imageControl.Source = source; 
      } 
   } 
 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Windows.Media 
 
Private Sub ConvertToSourceExample(ByVal imageControl As System.Windows.Controls.Image) 
   Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp") 
 
 
   Using codecs As New RasterCodecs() 
      ' Load the image 
      Using rasterImage As RasterImage = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.Bgr, 1, 1) 
         ' Convert it to WPF/Silverlight ImageSource 
         Dim source As System.Windows.Media.ImageSource = RasterImageConverter.ConvertToSource(rasterImage, ConvertToSourceOptions.None) 
 
         ' Set it in the Image control 
         imageControl.Source = source 
      End Using 
   End Using 
 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.Windows.Media; 
 
private void ConvertToSourceExample(System.Windows.Controls.Image imageControl) 
{ 
   string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
 
   RasterCodecs codecs = new RasterCodecs(); 
   // Load the image 
   using (RasterImage rasterImage = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.Bgr, 1, 1)) 
   { 
      // Convert it to WPF/Silverlight ImageSource 
      System.Windows.Media.ImageSource source = RasterImageConverter.ConvertToSource(rasterImage, ConvertToSourceOptions.None); 
 
      // Set it in the Image control 
      imageControl.Source = source; 
   } 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.Windows.Media 
Imports Leadtools.ImageProcessing.Color 
 
Private Sub ConvertToSourceExample(ByVal imageControl As System.Windows.Controls.Image) 
   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path & "Image1.cmp" 
 
   Dim codecs As RasterCodecs = New RasterCodecs() 
   ' Load the image 
   Using rasterImage As RasterImage = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.Bgr, 1, 1) 
      ' Convert it to WPF/Silverlight ImageSource 
      Dim source As System.Windows.Media.ImageSource = RasterImageConverter.ConvertToSource(rasterImage, ConvertToSourceOptions.None) 
 
      ' Set it in the Image control 
      imageControl.Source = source 
   End Using 
End Sub 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.