S
Sam
I posted this on the csharp group, but no response, so I'll try here.
I'm trying to convert some code I've downloaded from
http://www.codeproject.com/cs/miscctrl/CsExRichTextBox.asp
What it does is insert an image to a RichTextBox w/o using the
clipboard.
I've converted the code to VB.Net, but I'm getting a
System.ArgumentException with Additional information: Invalid
parameter used.
I then tried converting it using some converters on the web, but got the same error.
Here's the function it's happening in, and I've marked the line that
I'm getting the error on.
Any suggestions???
Private Function GetRTFImage(ByVal UseImage As Image) As String
Dim sbRTF As StringBuilder = Nothing
Dim ioMemoryStream As MemoryStream
Dim g As Graphics
Dim mfMetaFile As Metafile
Dim ptrHDC As IntPtr
Dim ptrHEmf As IntPtr
Try
sbRTF = New StringBuilder
ioMemoryStream = New MemoryStream
' Get a graphics context from the RichTextBox
g = Me.CreateGraphics
' Get the device context from the graphics context
ptrHDC = g.GetHdc()
' Create a new Enhanced Metafile from the device context
mfMetaFile = New Metafile(ioMemoryStream, ptrHDC)
' Release the device context
'g.ReleaseHdc(ptrHDC)
' Get a graphics context from the Enhanced Metafile
g = Graphics.FromImage(mfMetaFile)
' Draw the image on the Enhanced Metafile
g.DrawImage(UseImage, New Rectangle(0, 0, _
UseImage.Width, UseImage.Height))
'****FAILS ON THIS LINE OF CODE*****
' Get the handle of the Enhanced Metafile
ptrHEmf = mfMetaFile.GetHenhmetafile()
'***********************************
'A call to EmfToWmfBits with a null buffer return
'the size of the buffer need to store the WMF bits.
'Use Me to get the buffer size.
Dim uiBufferSize As UInt32
uiBufferSize = GdipEmfToWmfBits(ptrHEmf, _
uiBufferSize, Nothing, MM_ANISOTROPIC, _
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
' Create an array to hold the bits
Dim bytBufferArray As Byte()
'A call to EmfToWmfBits with a valid buffer
'copies the bits into the buffer an returns the
'number of bits in the WMF.
Dim uiConvertedSize As UInt32
uiConvertedSize = GdipEmfToWmfBits(ptrHEmf, _
uiBufferSize, bytBufferArray, _
MM_ANISOTROPIC, _
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
' Append the bits to the RTF string
Dim index As Integer = 0
Dim bytChar As Byte
For Each bytChar In bytBufferArray
sbRTF.Append(String.Format("{0:X2}", bytChar))
Next
Return sbRTF.ToString()
Finally
If (Not g Is Nothing) Then
g.Dispose()
End If
If (Not mfMetaFile Is Nothing) Then
mfMetaFile.Dispose()
End If
If (Not ioMemoryStream Is Nothing) Then
ioMemoryStream.Close()
End If
End Try
End Function
I'm trying to convert some code I've downloaded from
http://www.codeproject.com/cs/miscctrl/CsExRichTextBox.asp
What it does is insert an image to a RichTextBox w/o using the
clipboard.
I've converted the code to VB.Net, but I'm getting a
System.ArgumentException with Additional information: Invalid
parameter used.
I then tried converting it using some converters on the web, but got the same error.
Here's the function it's happening in, and I've marked the line that
I'm getting the error on.
Any suggestions???
Private Function GetRTFImage(ByVal UseImage As Image) As String
Dim sbRTF As StringBuilder = Nothing
Dim ioMemoryStream As MemoryStream
Dim g As Graphics
Dim mfMetaFile As Metafile
Dim ptrHDC As IntPtr
Dim ptrHEmf As IntPtr
Try
sbRTF = New StringBuilder
ioMemoryStream = New MemoryStream
' Get a graphics context from the RichTextBox
g = Me.CreateGraphics
' Get the device context from the graphics context
ptrHDC = g.GetHdc()
' Create a new Enhanced Metafile from the device context
mfMetaFile = New Metafile(ioMemoryStream, ptrHDC)
' Release the device context
'g.ReleaseHdc(ptrHDC)
' Get a graphics context from the Enhanced Metafile
g = Graphics.FromImage(mfMetaFile)
' Draw the image on the Enhanced Metafile
g.DrawImage(UseImage, New Rectangle(0, 0, _
UseImage.Width, UseImage.Height))
'****FAILS ON THIS LINE OF CODE*****
' Get the handle of the Enhanced Metafile
ptrHEmf = mfMetaFile.GetHenhmetafile()
'***********************************
'A call to EmfToWmfBits with a null buffer return
'the size of the buffer need to store the WMF bits.
'Use Me to get the buffer size.
Dim uiBufferSize As UInt32
uiBufferSize = GdipEmfToWmfBits(ptrHEmf, _
uiBufferSize, Nothing, MM_ANISOTROPIC, _
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
' Create an array to hold the bits
Dim bytBufferArray As Byte()
'A call to EmfToWmfBits with a valid buffer
'copies the bits into the buffer an returns the
'number of bits in the WMF.
Dim uiConvertedSize As UInt32
uiConvertedSize = GdipEmfToWmfBits(ptrHEmf, _
uiBufferSize, bytBufferArray, _
MM_ANISOTROPIC, _
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
' Append the bits to the RTF string
Dim index As Integer = 0
Dim bytChar As Byte
For Each bytChar In bytBufferArray
sbRTF.Append(String.Format("{0:X2}", bytChar))
Next
Return sbRTF.ToString()
Finally
If (Not g Is Nothing) Then
g.Dispose()
End If
If (Not mfMetaFile Is Nothing) Then
mfMetaFile.Dispose()
End If
If (Not ioMemoryStream Is Nothing) Then
ioMemoryStream.Close()
End If
End Try
End Function