←Select platform

Is this page helpful?

In this page

FromEmf(IntPtr,​int,​int) Method

Summary
Converts an Enhanced Windows Metafile (EMF) into a LEADTOOLS Leadtools.RasterImage object.
Syntax
C#
VB
C++
public static RasterImage FromEmf( 
   IntPtr hemf, 
   int width, 
   int height 
) 
Public Overloads Shared Function FromEmf( _ 
   ByVal hemf As IntPtr, _ 
   ByVal width As Integer, _ 
   ByVal height As Integer _ 
) As RasterImage 
public: 
static RasterImage^ FromEmf(  
   IntPtr hemf, 
   int width, 
   int height 
)  

Parameters

hemf
Handle to the EMF to be converted.

width
Amount by which to scale the enhanced metafiles original width.

height
Amount by which to scale the enhanced metafiles original height.

Return Value

The newly created Leadtools.RasterImage object.

Remarks

Some metafiles do not have a background color, and hence, when the metafile is used to create a Leadtools.RasterImage object, the area not covered by the metafile objects will contain the default image background color (black). To create a raster image from a metafile like this with a specific background color, use FromEmf(hemf, width, height, backColor).

When this method is completed, there are two copies of the drawing in memory: the new Leadtools.RasterImage object and the original EMF. Freeing one will not affect the other.

The enhanced metafile can be loaded at the original dimension or scaled by using the width and height parameters.

If width == 0 and height == 0 - the enhanced metafile is loaded at the size present in the file.

If width == 0 and height > 0 - the enhanced metafile is stretched so that it has the height height (preserving the aspect ratio).

If width > 0 and height == 0 - the enhanced metafile is stretched so that it has the width width (preserving the aspect ratio).

If width > 0 and height > 0 - the enhanced metafile is stretched so that it has the width width and height height (the aspect ratio is ignored).

This method does not support signed images.

For more information refer to RasterImage and GDI/GDI+.

Example

This example loads a Leadtools.RasterImage, converts it to an EMF, then converts the EMF back to a Leadtools.RasterImage.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
 
[DllImport("Gdi32", CharSet = CharSet.Auto)] 
private static extern int DeleteEnhMetaFile(IntPtr hemf); 
 
public void FromEmfExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   IntPtr hemf; 
 
   // Load an image 
   using (RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"), 24, CodecsLoadByteOrder.BgrOrGray, 1, 1)) 
   { 
      // Convert it to EMF 
      hemf = RasterImageConverter.ToEmf(image); 
 
      // Note, since we converted to an EMF we have two copies of the image in memory and "image" is still usable 
   } 
 
   // Convert the EMF back to a RasterImage preserving the size 
   using (RasterImage image = RasterImageConverter.FromEmf(hemf, 0, 0)) 
   { 
      // Not since we converted from the EMF, we need to delete it ourselves 
      DeleteEnhMetaFile(hemf); 
 
      // Save it to disk 
      codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "Image1_FromEmf.bmp"), RasterImageFormat.Bmp, 24); 
   } 
 
   // Clean up 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Drawing 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
 
<DllImport("Gdi32", CharSet:=CharSet.Auto)> 
Private Shared Function DeleteEnhMetaFile(ByVal hemf As IntPtr) As Integer 
End Function 
 
Public Sub FromEmfExample() 
   Dim codecs As New RasterCodecs() 
 
   Dim hemf As IntPtr 
 
   ' Load an image 
   Using image As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"), 24, CodecsLoadByteOrder.BgrOrGray, 1, 1) 
      ' Convert it to EMF 
      hemf = RasterImageConverter.ToEmf(image) 
 
      ' Note, since we converted to an EMF we have two copies of the image in memory and "image" is still usable 
   End Using 
 
   ' Convert the EMF back to a RasterImage preserving the size 
   Using image As RasterImage = RasterImageConverter.FromEmf(hemf, 0, 0) 
      ' Not since we converted from the EMF, we need to delete it ourselves 
      DeleteEnhMetaFile(hemf) 
 
      ' Save it to disk 
      codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "Image1_FromEmf.bmp"), RasterImageFormat.Bmp, 24) 
   End Using 
 
   ' Clean up 
   codecs.Dispose() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images" 
End Class 
Requirements

Target Platforms

Help Version 21.0.2021.7.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Drawing Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.