←Select platform

UpdateImageFromSource Method

Summary

Converts the WPF/Silverlight image in the Source property to the LEADTOOLS Leadtools.RasterImage and set it in Image.

Syntax

C#
VB
C++
public virtual void UpdateImageFromSource() 
  
Public Overridable Sub UpdateImageFromSource()  
public: 
virtual void UpdateImageFromSource();  

Remarks

If the Source property is changed, the RasterImageViewer must be informed so it can reflect the changes into the Image object. The UpdateImageFromSource can be used to perform this task.

The control uses the RasterImageConverter.ConvertFromSource to convert the WPF/Silverlight System.Windows.Media.ImageSource to LEADTOOLS Leadtools.RasterImage. The value of the ConvertFromSourceOptions property is used as the options parameter to this method.

Example

C#
VB
Silverlight C#
Silverlight VB
using Leadtools.Help; 
using Leadtools.Windows.Controls; 
using Leadtools.ImageProcessing; 
using Leadtools.Codecs; 
 
public void ImageViewer_UpdateImageFromSource(RasterImageViewer viewer) 
{ 
   BitmapImage bitmapImage = new BitmapImage(new Uri(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg"))); 
 
   //Convert to Bgr32a 
   FormatConvertedBitmap convertedBitmap = new FormatConvertedBitmap(bitmapImage, PixelFormats.Bgra32, null, 1); 
 
   viewer.Source = new WriteableBitmap(convertedBitmap); 
 
   WriteableBitmap writeableBitmap = viewer.Source as WriteableBitmap; 
 
   int[] pixels = new int[writeableBitmap.PixelWidth * writeableBitmap.PixelHeight]; 
   int stride = writeableBitmap.PixelWidth * 4; 
 
   writeableBitmap.CopyPixels(pixels, stride, 0); 
 
   // Fill the bitmap with Red. 
   for (int i = 0; i < writeableBitmap.PixelWidth; ++i) 
   { 
      for (int j = 0; j < writeableBitmap.PixelHeight; ++j) 
      { 
         int color_data = (255 << 24) | (0 << 16) | (0 << 8) | 0; 
         int pos = i + j * writeableBitmap.PixelWidth; 
         pixels[pos] = color_data; 
      } 
   } 
 
   Int32Rect sourceRect = new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight); 
   writeableBitmap.WritePixels(sourceRect, pixels, stride, 0); 
 
   viewer.UpdateImageFromSource(); 
} 
 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools.Windows.Controls 
Imports Leadtools.ImageProcessing 
Imports Leadtools.Codecs 
Imports Leadtools 
 
Public Sub ImageViewer_UpdateImageFromSource(ByVal viewer As RasterImageViewer) 
   Dim bitmapImage As BitmapImage = New BitmapImage(New Uri(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg"))) 
 
   'Convert to Bgr32a 
   Dim convertedBitmap As FormatConvertedBitmap = New FormatConvertedBitmap(bitmapImage, PixelFormats.Bgra32, Nothing, 1) 
 
   viewer.Source = New WriteableBitmap(convertedBitmap) 
 
   Dim writeableBitmap As WriteableBitmap = TryCast(viewer.Source, WriteableBitmap) 
 
   Dim pixels As Integer() = New Integer(writeableBitmap.PixelWidth * writeableBitmap.PixelHeight - 1) {} 
   Dim stride As Integer = writeableBitmap.PixelWidth * 4 
 
   writeableBitmap.CopyPixels(pixels, stride, 0) 
 
   ' Fill the bitmap with Red. 
   Dim i As Integer = 0 
   Do While i < writeableBitmap.PixelWidth 
      Dim j As Integer = 0 
      Do While j < writeableBitmap.PixelHeight 
         Dim color_data As Integer = (255 << 24) Or (0 << 16) Or (0 << 8) Or 0 
         Dim pos As Integer = i + j * writeableBitmap.PixelWidth 
         pixels(pos) = color_data 
         j += 1 
      Loop 
      i += 1 
   Loop 
 
   Dim sourceRect As Int32Rect = New Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight) 
   writeableBitmap.WritePixels(sourceRect, pixels, stride, 0) 
 
   viewer.UpdateImageFromSource() 
End Sub 
 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 
using Leadtools.Help; 
using Leadtools.Windows.Controls; 
using Leadtools.ImageProcessing; 
using Leadtools.Codecs; 
 
public void ImageViewer_UpdateImageFromSource(RasterImageViewer viewer) 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
   RasterImage image = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path + "cannon.jpg"); 
 
   //Convert to Bgr32a 
   ColorResolutionCommand cmd = new ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 32, RasterByteOrder.Bgr, RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.None, null); 
   cmd.Run(image); 
 
   Stream stream = new MemoryStream(); 
   codecs.Save(image, stream, RasterImageFormat.Bmp, image.BitsPerPixel); 
   BitmapImage imgSource = new BitmapImage(); 
   imgSource.SetSource(stream); 
   viewer.Source = imgSource; 
 
   WriteableBitmap writeableBitmap = viewer.Source as WriteableBitmap; 
 
   int[] pixels = new int[writeableBitmap.PixelWidth * writeableBitmap.PixelHeight]; 
   int stride = writeableBitmap.PixelWidth * 4; 
 
   Array.Copy(writeableBitmap.Pixels, pixels, stride); 
 
   // Fill the bitmap with Red. 
   for (int i = 0; i < writeableBitmap.PixelWidth; ++i) 
   { 
      for (int j = 0; j < writeableBitmap.PixelHeight; ++j) 
      { 
         int color_data = (255 << 24) | (0 << 16) | (0 << 8) | 0; 
         int pos = i + j * writeableBitmap.PixelWidth; 
         pixels[pos] = color_data; 
      } 
   } 
 
   Array.Copy(pixels, writeableBitmap.Pixels, stride); 
 
   viewer.UpdateImageFromSource(); 
} 
Imports Leadtools 
Imports Leadtools.Windows.Controls 
Imports Leadtools.ImageProcessing 
Imports Leadtools.Codecs 
 
Public Sub ImageViewer_UpdateImageFromSource(ByVal viewer As RasterImageViewer) 
   Dim codecs As RasterCodecs = New RasterCodecs() 
   Dim image As RasterImage = codecs.Load(LeadtoolsExamples.Common.ImagesPath.Path & "cannon.jpg") 
 
   'Convert to Bgr32a 
   Dim cmd As ColorResolutionCommand = New ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 32, RasterByteOrder.Bgr, 
                                                                  RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.None, Nothing) 
   cmd.Run(image) 
 
   Dim stream As Stream = New MemoryStream() 
   codecs.Save(image, stream, RasterImageFormat.Bmp, image.BitsPerPixel) 
   Dim imgSource As BitmapImage = New BitmapImage() 
   imgSource.SetSource(stream) 
   viewer.Source = imgSource 
 
   Dim writeableBitmap As WriteableBitmap = TryCast(viewer.Source, WriteableBitmap) 
 
   Dim pixels As Integer() = New Integer(writeableBitmap.PixelWidth * writeableBitmap.PixelHeight - 1) {} 
   Dim stride As Integer = writeableBitmap.PixelWidth * 4 
 
   Array.Copy(writeableBitmap.Pixels, pixels, stride) 
 
   ' Fill the bitmap with Red. 
   Dim i As Integer = 0 
   Do While i < writeableBitmap.PixelWidth 
      Dim j As Integer = 0 
      Do While j < writeableBitmap.PixelHeight 
         Dim color_data As Integer = (255 << 24) Or (0 << 16) Or (0 << 8) Or 0 
         Dim pos As Integer = i + j * writeableBitmap.PixelWidth 
         pixels(pos) = color_data 
         j += 1 
      Loop 
      i += 1 
   Loop 
 
   Array.Copy(pixels, writeableBitmap.Pixels, stride) 
 
   viewer.UpdateImageFromSource() 
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.

Leadtools.Windows.Controls Assembly