none
INSERTAR IMAGEN EN RICHTEXTBOX SIN USAR PORTAPAPELES RRS feed

  • Pregunta

  • Hola, tengo una aplicación en VB.NET que reemplaza determinados textos de un RTF. Uno de esos textos, debe ser reemplazado por una imagen que puede ser jpg, gif, bmp, png o tiff. Busqué en internet y aquí en el foro, pero ninguno de los ejemplos que he encontrado funciona bien. Alguno tendrá código para hacer esto que actualmente funcione?

     

    Gracias.

    • Cambiado Enrique M. Montejo sábado, 30 de julio de 2011 10:53 Controles de Windows Forms (De:Lenguaje VB.NET)
    jueves, 14 de julio de 2011 17:18

Respuestas

  • Logré hacerlo funcionar!! tenía mal el código. Por lo visto, cuando lo traduje de C#, lo hice mal. Por si le interesa a alguno, dejo el código para VB.NET:

     

    Imports System

    Imports System.Windows.Forms

    Imports System.Drawing

    Imports System.Drawing.Imaging

    Imports System.Drawing.Printing

    Imports System.Runtime.InteropServices

    Imports System.Text

    Imports System.IO

    Public Class clsRichTextBox

    Inherits RichTextBox

    Private Const HMM_PER_INCH As Integer = 2540 'The number of hundredths of millimeters (0.01 mm) in an inch

    Private Const TWIPS_PER_INCH As Integer = 1440 'The number of twips in an inch

    Private Const MM_ANISOTROPIC As Integer = 8 'Allows the x and y coordinates of the metafile to be adjusted independently

    ''' <summary>

    ''' Use the EmfToWmfBits function in the GDI+ specification to convert a 

    ''' Enhanced Metafile to a Windows Metafile

    ''' </summary>

    ''' <param name="pobj_hEmf">A handle to the Enhanced Metafile to be converted</param>

    ''' <param name="pvar_BufferSize">The size of the buffer used to store the Windows Metafile bits returned</param>

    ''' <param name="pvar_Buffer">An array of bytes used to hold the Windows Metafile bits returned</param>

    ''' <param name="pvar_MappingMode">The mapping mode of the image.  This control uses MM_ANISOTROPIC.</param>

    ''' <param name="pvar_Flags">Flags used to specify the format of the Windows Metafile returned</param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    <DllImportAttribute("gdiplus.dll")> _

    Private Shared Function GdipEmfToWmfBits(ByVal pobj_hEmf As IntPtr, ByVal pvar_BufferSize As UInteger, ByVal pvar_Buffer As Byte(), ByVal pvar_MappingMode As Integer, ByVal pvar_Flags As EmfToWmfBitsFlags) As UInteger

    End Function

    Private Enum EmfToWmfBitsFlags

    '// Use the default conversion

    EmfToWmfBitsFlagsDefault = &H0

     

    '// Embedded the source of the EMF metafile within the resulting WMF

    '// metafile

    EmfToWmfBitsFlagsEmbedEmf = &H1

     

    '// Place a 22-byte header in the resulting WMF file. The header is

    '// required for the metafile to be considered placeable.

    EmfToWmfBitsFlagsIncludePlaceable = &H2

     

    '// Don't simulate clipping by using the XOR operator.

    EmfToWmfBitsFlagsNoXORClip = &H4

    End Enum

    ''' <summary>

    ''' Creates the RTF control string that describes the image being inserted.

    ''' This description (in this case) specifies that the image is an

    ''' MM_ANISOTROPIC metafile, meaning that both X and Y axes can be scaled

    ''' independently.  The control string also gives the images current dimensions,

    ''' and its target dimensions, so if you want to control the size of the

    ''' image being inserted, this would be the place to do it. The prefix should

    ''' have the form ...

    ''' 

    ''' {\pict\wmetafile8\picw[A]\pich[B]\picwgoal[C]\pichgoal[D]

    ''' 

    ''' where ...

    ''' 

    ''' A = current width of the metafile in hundredths of millimeters (0.01mm)

    ''' = Image Width in Inches * Number of (0.01mm) per inch

    ''' = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 2540

    ''' = (Image Width in Pixels / Graphics.DpiX) * 2540

    ''' 

    ''' B = current height of the metafile in hundredths of millimeters (0.01mm)

    ''' = Image Height in Inches * Number of (0.01mm) per inch

    ''' = (Image Height in Pixels / Graphics Context's Vertical Resolution) * 2540

    ''' = (Image Height in Pixels / Graphics.DpiX) * 2540

    ''' 

    ''' C = target width of the metafile in twips

    ''' = Image Width in Inches * Number of twips per inch

    ''' = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 1440

    ''' = (Image Width in Pixels / Graphics.DpiX) * 1440

    ''' 

    ''' D = target height of the metafile in twips

    ''' = Image Height in Inches * Number of twips per inch

    ''' = (Image Height in Pixels / Graphics Context's Horizontal Resolution) * 1440

    ''' = (Image Height in Pixels / Graphics.DpiX) * 1440

    ''' 

    ''' </summary>

    ''' <param name="pvar_Texto">Text to replace</param>

    ''' <param name="pobj_Image">The object of the image to replace the text with</param>

    ''' <remarks>

    ''' The Graphics Context's resolution is simply the current resolution at which

    ''' windows is being displayed.  Normally it's 96 dpi, but instead of assuming

    ''' I just added the code.

    ''' 

    ''' According to Ken Howe at pbdr.com, "Twips are screen-independent units

    ''' used to ensure that the placement and proportion of screen elements in

    ''' your screen application are the same on all display systems."

    ''' 

    ''' Units Used

    ''' ----------

    ''' 1 Twip = 1/20 Point

    ''' 1 Point = 1/72 Inch

    ''' 1 Twip = 1/1440 Inch

    ''' 

    ''' 1 Inch = 2.54 cm

    ''' 1 Inch = 25.4 mm

    ''' 1 Inch = 2540 (0.01)mm

    ''' 

    ''' Código obtenido de "http://www.codeproject.com/KB/edit/csexrichtextbox.aspx"

    ''' 

    ''' </remarks>

    Public Sub InsertarImagen(ByVal pvar_Texto As String, ByVal pobj_Image As Image)

    Dim lvar_Start As Integer = Me.Rtf.IndexOf(pvar_Texto)

    Dim lvar_Finish As Integer = lvar_Start + pvar_Texto.Length

    Dim lvar_RTFImagen As StringBuilder

     

    If lvar_Start > 0 Then

    lvar_RTFImagen = New StringBuilder

    lvar_RTFImagen.Append("{\pict\wmetafile8")

    lvar_RTFImagen.Append(ObtenerDimensionesImagen(pobj_Image))

    lvar_RTFImagen.Append(" ")

    lvar_RTFImagen.Append(ObtenerImagenRTF(pobj_Image))

    lvar_RTFImagen.Append("}")

     

    Me.Rtf = Me.Rtf.Substring(0, lvar_Start) + lvar_RTFImagen.ToString + Me.Rtf.Substring(lvar_Finish)

    End If

    End Sub

     

    Private Function ObtenerDimensionesImagen(ByVal pobj_Imagen As Image) As String

     

    Dim lvar_RTFImagen As New StringBuilder

    Dim lobj_Graphics As Graphics = Me.CreateGraphics()

    Dim lvar_DpiX As Single = lobj_Graphics.DpiX

    Dim lvar_DpiY As Single = lobj_Graphics.DpiY

     

    ' Calculate the current width of the image in (0.01)mm

    Dim lvar_picw As Integer = CType(Math.Round((pobj_Imagen.Width / lvar_DpiX) * HMM_PER_INCH), Integer)

     

    ' Calculate the current height of the image in (0.01)mm

    Dim lvar_pich As Integer = CType(Math.Round((pobj_Imagen.Height / lvar_DpiY) * HMM_PER_INCH), Integer)

     

    ' Calculate the target width of the image in twips

    Dim lvar_picwgoal As Integer = CType(Math.Round((pobj_Imagen.Width / lvar_DpiX) * TWIPS_PER_INCH), Integer)

     

    ' Calculate the target height of the image in twips

    Dim lvar_pichgoal As Integer = CType(Math.Round((pobj_Imagen.Height / lvar_DpiY) * TWIPS_PER_INCH), Integer)

     

    ' Append values to RTF string

    lvar_RTFImagen.Append("\picw")

    lvar_RTFImagen.Append(lvar_picw.ToString)

    lvar_RTFImagen.Append("\pich")

    lvar_RTFImagen.Append(lvar_pich.ToString)

    lvar_RTFImagen.Append("\picwgoal")

    lvar_RTFImagen.Append(lvar_picwgoal.ToString)

    lvar_RTFImagen.Append("\pichgoal")

    lvar_RTFImagen.Append(lvar_pichgoal.ToString)

     

    Return lvar_RTFImagen.ToString()

    End Function

     

    ''' <summary>

    ''' Wraps the image in an Enhanced Metafile by drawing the image onto the

    ''' graphics context, then converts the Enhanced Metafile to a Windows

    ''' Metafile, and finally appends the bits of the Windows Metafile in HEX

    ''' to a string and returns the string.

    ''' </summary>

    ''' <param name="pobj_Imagen">The object of the image to replace the text with</param>

    ''' <returns>A string containing the bits of a Windows Metafile in HEX</returns>

    ''' <remarks></remarks>

    Private Function ObtenerImagenRTF(ByVal pobj_Imagen As Image) As String

     

    Dim lvar_ReturnValue As String = ""

    Dim lobj_MemoryStream As MemoryStream = Nothing 'Used to store the enhanced metafile

    Dim lobj_MetaFile As Metafile = Nothing 'The enhanced metafile

    Dim lobj_HDC As IntPtr 'Handle to the device context used to create the metafile

    Dim lobj_hEmf As IntPtr

    Dim lvar_BufferSize As UInteger = 0

    Dim lvar_ConvertedSize As UInteger = 0

    Dim lvar_Buffer As Byte() = Nothing

     

    Try

    ' Allocate memory to hold Metafile

    lobj_MemoryStream = New MemoryStream

     

    ' Get a graphics context from the RichTextBox

    Using lobj_Graphics As Graphics = Me.CreateGraphics()

    ' Get the device context from the graphics context

    lobj_HDC = lobj_Graphics.GetHdc()

    ' Create a new Enhanced Metafile from the device context

    lobj_MetaFile = New Metafile(lobj_MemoryStream, lobj_HDC)

    ' Release the device context

    lobj_Graphics.ReleaseHdc(lobj_HDC)

    End Using

     

    ' Get a graphics context from the Enhanced Metafile

    Using lobj_Graphics As Graphics = Graphics.FromImage(lobj_MetaFile)

    ' Draw the image on the Enhanced Metafile

    lobj_Graphics.DrawImage(pobj_Imagen, New Rectangle(0, 0, pobj_Imagen.Width, pobj_Imagen.Height))

    End Using

     

    ' Get the handle of the Enhanced Metafile

    lobj_hEmf = lobj_MetaFile.GetHenhmetafile

     

    ' A call to EmfToWmfBits with a null buffer return the size of the

    ' buffer need to store the WMF bits.  Use this to get the buffer

    ' size.

    lvar_BufferSize = GdipEmfToWmfBits(lobj_hEmf, 0, Nothing, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)

     

    ' Create an array to hold the bits

    lvar_Buffer = New Byte(lvar_BufferSize - 1) {}

     

    ' A call to EmfToWmfBits with a valid buffer copies the bits into the

    ' buffer an returns the number of bits in the WMF.  

    lvar_ConvertedSize = GdipEmfToWmfBits(lobj_hEmf, lvar_BufferSize, lvar_Buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)

     

    ' Convert Bits to Hex

    lvar_ReturnValue = BitConverter.ToString(lvar_Buffer, 0).Replace("-", String.Empty)

     

    Finally

    If lobj_MetaFile IsNot Nothing Then lobj_MetaFile.Dispose()

    If lobj_MemoryStream IsNot Nothing Then lobj_MemoryStream.Close()

    End Try

     

     

    Return lvar_ReturnValue

     

    End Function

    End Class

     

    sábado, 30 de julio de 2011 22:12

Todas las respuestas

  • Julio, gracias por responder. Noto por la respuesta que no me he expresado correctamente. Lo que necesito no es reemplazar un texto por una única imagen, lo que necesito es reemplazarlo por una imagen X que se encontrará en un repositorio de imágenes. Por ejemplo, supongamos que debo imprimir correo (no e-mail) y dentro de este correo escribo algo como lo siguiente (así se vería el RTF que armo):

     

    Buenos días Sr/Sra {NOMBRECLIENTE},

     

    Su pedido será entregado en {DIASENTREGA} días por {NOMBRE_REPARTIDOR} {FOTO_REPARTIDOR}

     

    Luego, tengo un programa en VB.NET que tiene un objeto RichTextBox y lo que quiero hacer, es algo como lo siguiente:

     

    for each lvar_Cliente as string in lvar_ListaClientes

    mobj_Document.RTF= mobj_Document.RTF.replace(“{NOMBRECLIENTE}”,GetClientName(lvar_Cliente)) à Esto funciona bien

    mobj_Document.RTF= mobj_Document.RTF.replace(“{DIASENTREGA }”,”30”) à Esto funciona bien

    mobj_Document.RTF= mobj_Document.RTF.replace(“{NOMBRE_REPARTIDOR}”,GetAssignedDeliveryName(lvar_Cliente)) à Esto funciona bien

    mobj_Document.RTF= mobj_Document.RTF.replace(“{FOTO_REPARTIDOR}”,GetAssignedDeliveryImage(lvar_Cliente)) à Esto NO funciona

    next

     

    La idea es que la función “GetAssignedDeliveryImage”, logre obtener de la base de datos el nombre de la imagen (por ejemplo “Alberto.gif”. Esto lo hace bien) y luego vaya a una carpeta “X” en el servidor a buscar dicha imagen (esto también lo hace bien) para insertarla en el objeto RichTextBox (esto es lo que no hace bien).

     

    El código que he conseguido como ejemplo es el publicado en los siguientes links:

     

    http://stackoverflow.com/questions/1490734/programatically-adding-images-to-rtf-document

    http://www.biblioscape.com/rtf15_spec.htm#Heading49

    http://msdn.microsoft.com/en-us/library/aa140283(v=office.10).aspx#rtfspec_24

    viernes, 22 de julio de 2011 14:45
  • Logré hacerlo funcionar!! tenía mal el código. Por lo visto, cuando lo traduje de C#, lo hice mal. Por si le interesa a alguno, dejo el código para VB.NET:

     

    Imports System

    Imports System.Windows.Forms

    Imports System.Drawing

    Imports System.Drawing.Imaging

    Imports System.Drawing.Printing

    Imports System.Runtime.InteropServices

    Imports System.Text

    Imports System.IO

    Public Class clsRichTextBox

    Inherits RichTextBox

    Private Const HMM_PER_INCH As Integer = 2540 'The number of hundredths of millimeters (0.01 mm) in an inch

    Private Const TWIPS_PER_INCH As Integer = 1440 'The number of twips in an inch

    Private Const MM_ANISOTROPIC As Integer = 8 'Allows the x and y coordinates of the metafile to be adjusted independently

    ''' <summary>

    ''' Use the EmfToWmfBits function in the GDI+ specification to convert a 

    ''' Enhanced Metafile to a Windows Metafile

    ''' </summary>

    ''' <param name="pobj_hEmf">A handle to the Enhanced Metafile to be converted</param>

    ''' <param name="pvar_BufferSize">The size of the buffer used to store the Windows Metafile bits returned</param>

    ''' <param name="pvar_Buffer">An array of bytes used to hold the Windows Metafile bits returned</param>

    ''' <param name="pvar_MappingMode">The mapping mode of the image.  This control uses MM_ANISOTROPIC.</param>

    ''' <param name="pvar_Flags">Flags used to specify the format of the Windows Metafile returned</param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    <DllImportAttribute("gdiplus.dll")> _

    Private Shared Function GdipEmfToWmfBits(ByVal pobj_hEmf As IntPtr, ByVal pvar_BufferSize As UInteger, ByVal pvar_Buffer As Byte(), ByVal pvar_MappingMode As Integer, ByVal pvar_Flags As EmfToWmfBitsFlags) As UInteger

    End Function

    Private Enum EmfToWmfBitsFlags

    '// Use the default conversion

    EmfToWmfBitsFlagsDefault = &H0

     

    '// Embedded the source of the EMF metafile within the resulting WMF

    '// metafile

    EmfToWmfBitsFlagsEmbedEmf = &H1

     

    '// Place a 22-byte header in the resulting WMF file. The header is

    '// required for the metafile to be considered placeable.

    EmfToWmfBitsFlagsIncludePlaceable = &H2

     

    '// Don't simulate clipping by using the XOR operator.

    EmfToWmfBitsFlagsNoXORClip = &H4

    End Enum

    ''' <summary>

    ''' Creates the RTF control string that describes the image being inserted.

    ''' This description (in this case) specifies that the image is an

    ''' MM_ANISOTROPIC metafile, meaning that both X and Y axes can be scaled

    ''' independently.  The control string also gives the images current dimensions,

    ''' and its target dimensions, so if you want to control the size of the

    ''' image being inserted, this would be the place to do it. The prefix should

    ''' have the form ...

    ''' 

    ''' {\pict\wmetafile8\picw[A]\pich[B]\picwgoal[C]\pichgoal[D]

    ''' 

    ''' where ...

    ''' 

    ''' A = current width of the metafile in hundredths of millimeters (0.01mm)

    ''' = Image Width in Inches * Number of (0.01mm) per inch

    ''' = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 2540

    ''' = (Image Width in Pixels / Graphics.DpiX) * 2540

    ''' 

    ''' B = current height of the metafile in hundredths of millimeters (0.01mm)

    ''' = Image Height in Inches * Number of (0.01mm) per inch

    ''' = (Image Height in Pixels / Graphics Context's Vertical Resolution) * 2540

    ''' = (Image Height in Pixels / Graphics.DpiX) * 2540

    ''' 

    ''' C = target width of the metafile in twips

    ''' = Image Width in Inches * Number of twips per inch

    ''' = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 1440

    ''' = (Image Width in Pixels / Graphics.DpiX) * 1440

    ''' 

    ''' D = target height of the metafile in twips

    ''' = Image Height in Inches * Number of twips per inch

    ''' = (Image Height in Pixels / Graphics Context's Horizontal Resolution) * 1440

    ''' = (Image Height in Pixels / Graphics.DpiX) * 1440

    ''' 

    ''' </summary>

    ''' <param name="pvar_Texto">Text to replace</param>

    ''' <param name="pobj_Image">The object of the image to replace the text with</param>

    ''' <remarks>

    ''' The Graphics Context's resolution is simply the current resolution at which

    ''' windows is being displayed.  Normally it's 96 dpi, but instead of assuming

    ''' I just added the code.

    ''' 

    ''' According to Ken Howe at pbdr.com, "Twips are screen-independent units

    ''' used to ensure that the placement and proportion of screen elements in

    ''' your screen application are the same on all display systems."

    ''' 

    ''' Units Used

    ''' ----------

    ''' 1 Twip = 1/20 Point

    ''' 1 Point = 1/72 Inch

    ''' 1 Twip = 1/1440 Inch

    ''' 

    ''' 1 Inch = 2.54 cm

    ''' 1 Inch = 25.4 mm

    ''' 1 Inch = 2540 (0.01)mm

    ''' 

    ''' Código obtenido de "http://www.codeproject.com/KB/edit/csexrichtextbox.aspx"

    ''' 

    ''' </remarks>

    Public Sub InsertarImagen(ByVal pvar_Texto As String, ByVal pobj_Image As Image)

    Dim lvar_Start As Integer = Me.Rtf.IndexOf(pvar_Texto)

    Dim lvar_Finish As Integer = lvar_Start + pvar_Texto.Length

    Dim lvar_RTFImagen As StringBuilder

     

    If lvar_Start > 0 Then

    lvar_RTFImagen = New StringBuilder

    lvar_RTFImagen.Append("{\pict\wmetafile8")

    lvar_RTFImagen.Append(ObtenerDimensionesImagen(pobj_Image))

    lvar_RTFImagen.Append(" ")

    lvar_RTFImagen.Append(ObtenerImagenRTF(pobj_Image))

    lvar_RTFImagen.Append("}")

     

    Me.Rtf = Me.Rtf.Substring(0, lvar_Start) + lvar_RTFImagen.ToString + Me.Rtf.Substring(lvar_Finish)

    End If

    End Sub

     

    Private Function ObtenerDimensionesImagen(ByVal pobj_Imagen As Image) As String

     

    Dim lvar_RTFImagen As New StringBuilder

    Dim lobj_Graphics As Graphics = Me.CreateGraphics()

    Dim lvar_DpiX As Single = lobj_Graphics.DpiX

    Dim lvar_DpiY As Single = lobj_Graphics.DpiY

     

    ' Calculate the current width of the image in (0.01)mm

    Dim lvar_picw As Integer = CType(Math.Round((pobj_Imagen.Width / lvar_DpiX) * HMM_PER_INCH), Integer)

     

    ' Calculate the current height of the image in (0.01)mm

    Dim lvar_pich As Integer = CType(Math.Round((pobj_Imagen.Height / lvar_DpiY) * HMM_PER_INCH), Integer)

     

    ' Calculate the target width of the image in twips

    Dim lvar_picwgoal As Integer = CType(Math.Round((pobj_Imagen.Width / lvar_DpiX) * TWIPS_PER_INCH), Integer)

     

    ' Calculate the target height of the image in twips

    Dim lvar_pichgoal As Integer = CType(Math.Round((pobj_Imagen.Height / lvar_DpiY) * TWIPS_PER_INCH), Integer)

     

    ' Append values to RTF string

    lvar_RTFImagen.Append("\picw")

    lvar_RTFImagen.Append(lvar_picw.ToString)

    lvar_RTFImagen.Append("\pich")

    lvar_RTFImagen.Append(lvar_pich.ToString)

    lvar_RTFImagen.Append("\picwgoal")

    lvar_RTFImagen.Append(lvar_picwgoal.ToString)

    lvar_RTFImagen.Append("\pichgoal")

    lvar_RTFImagen.Append(lvar_pichgoal.ToString)

     

    Return lvar_RTFImagen.ToString()

    End Function

     

    ''' <summary>

    ''' Wraps the image in an Enhanced Metafile by drawing the image onto the

    ''' graphics context, then converts the Enhanced Metafile to a Windows

    ''' Metafile, and finally appends the bits of the Windows Metafile in HEX

    ''' to a string and returns the string.

    ''' </summary>

    ''' <param name="pobj_Imagen">The object of the image to replace the text with</param>

    ''' <returns>A string containing the bits of a Windows Metafile in HEX</returns>

    ''' <remarks></remarks>

    Private Function ObtenerImagenRTF(ByVal pobj_Imagen As Image) As String

     

    Dim lvar_ReturnValue As String = ""

    Dim lobj_MemoryStream As MemoryStream = Nothing 'Used to store the enhanced metafile

    Dim lobj_MetaFile As Metafile = Nothing 'The enhanced metafile

    Dim lobj_HDC As IntPtr 'Handle to the device context used to create the metafile

    Dim lobj_hEmf As IntPtr

    Dim lvar_BufferSize As UInteger = 0

    Dim lvar_ConvertedSize As UInteger = 0

    Dim lvar_Buffer As Byte() = Nothing

     

    Try

    ' Allocate memory to hold Metafile

    lobj_MemoryStream = New MemoryStream

     

    ' Get a graphics context from the RichTextBox

    Using lobj_Graphics As Graphics = Me.CreateGraphics()

    ' Get the device context from the graphics context

    lobj_HDC = lobj_Graphics.GetHdc()

    ' Create a new Enhanced Metafile from the device context

    lobj_MetaFile = New Metafile(lobj_MemoryStream, lobj_HDC)

    ' Release the device context

    lobj_Graphics.ReleaseHdc(lobj_HDC)

    End Using

     

    ' Get a graphics context from the Enhanced Metafile

    Using lobj_Graphics As Graphics = Graphics.FromImage(lobj_MetaFile)

    ' Draw the image on the Enhanced Metafile

    lobj_Graphics.DrawImage(pobj_Imagen, New Rectangle(0, 0, pobj_Imagen.Width, pobj_Imagen.Height))

    End Using

     

    ' Get the handle of the Enhanced Metafile

    lobj_hEmf = lobj_MetaFile.GetHenhmetafile

     

    ' A call to EmfToWmfBits with a null buffer return the size of the

    ' buffer need to store the WMF bits.  Use this to get the buffer

    ' size.

    lvar_BufferSize = GdipEmfToWmfBits(lobj_hEmf, 0, Nothing, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)

     

    ' Create an array to hold the bits

    lvar_Buffer = New Byte(lvar_BufferSize - 1) {}

     

    ' A call to EmfToWmfBits with a valid buffer copies the bits into the

    ' buffer an returns the number of bits in the WMF.  

    lvar_ConvertedSize = GdipEmfToWmfBits(lobj_hEmf, lvar_BufferSize, lvar_Buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)

     

    ' Convert Bits to Hex

    lvar_ReturnValue = BitConverter.ToString(lvar_Buffer, 0).Replace("-", String.Empty)

     

    Finally

    If lobj_MetaFile IsNot Nothing Then lobj_MetaFile.Dispose()

    If lobj_MemoryStream IsNot Nothing Then lobj_MemoryStream.Close()

    End Try

     

     

    Return lvar_ReturnValue

     

    End Function

    End Class

     

    sábado, 30 de julio de 2011 22:12