Help converting C# to VB.Net - I'm doing something wrong.

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
 
S

sag

Hello,

I stepped into the same problem, too.
After having made some experiments I modified the code into this:

Private Function GetRtfImage(ByVal img As Image, ByVal lenBuff%) As String
Dim sb As New System.Text.StringBuilder()
Dim g As Graphics
Dim mFile As System.Drawing.Imaging.Metafile
Dim hDcRef As IntPtr
Dim hDcEmf, hEmf As IntPtr
Dim rc As API.RECT
Dim sizeBuff As UInt32
Dim intSizeBuff As Int32

Try
g = Me.CreateGraphics
rc.Right = (img.Width / g.DpiX) * HMM_PER_INCH
rc.Bottom = (img.Height / g.DpiY) * HMM_PER_INCH
hDcRef = g.GetHdc
hDcEmf = API.CreateEnhMetaFile(hDcRef, vbNullString, rc, "")
g.ReleaseHdc(hDcRef)
g.Dispose()

g = Graphics.FromHdc(hDcEmf)
g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height))
hEmf = API.CloseEnhMetaFile(hDcEmf)

'// 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.
sizeBuff = API.GdipEmfToWmfBits(hEmf, sizeBuff, Nothing, API.MM_ANISOTROPIC, API.EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
intSizeBuff = Convert.ToInt32(sizeBuff)
Dim buff(intSizeBuff - 1) As Byte

sizeBuff = API.GdipEmfToWmfBits(hEmf, sizeBuff, buff, API.MM_ANISOTROPIC, API.EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)
intSizeBuff = Convert.ToInt32(sizeBuff)
Dim idx%
For idx = 0 To intSizeBuff - 1
sb.Append(buff(idx).ToString("X2"))
Next


g.Dispose()

Return sb.ToString

Catch ex As Exception
Debug.Write(ex.Message)
End Try
End Function

The Api-Declarations are these:
Friend Class API
Public Declare Function CreateEnhMetaFile Lib "gdi32" Alias "CreateEnhMetaFileA" ( _
ByVal hdcRef As IntPtr, _
ByVal lpFileName As String, _
ByVal lpRect As RECT, _
ByVal lpDescription As String) As IntPtr

Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure

Public Declare Function CloseEnhMetaFile Lib "gdi32" ( _
ByVal hdc As IntPtr) As IntPtr

Public Enum EmfToWmfBitsFlags
'// Use the default conversion
EmfToWmfBitsFlagsDefault = 0

'// Embedded the source of the EMF metafiel within the resulting WMF
'// metafile
EmfToWmfBitsFlagsEmbedEmf = 1

'// Place a 22-byte header in the resulting WMF file. The header is
'// required for the metafile to be considered placeable.
EmfToWmfBitsFlagsIncludePlaceable = 2

'// Don't simulate clipping by using the XOR operator.
EmfToWmfBitsFlagsNoXORClip = 4
End Enum

'// Ensures that the metafile maintains a 1:1 aspect ratio
Public Const MM_ISOTROPIC% = 7

'// Allows the x-coordinates and y-coordinates of the metafile to be adjusted
'// independently
Public Const MM_ANISOTROPIC% = 8

<DllImport("gdiplus.dll")> _
Public Shared Function GdipEmfToWmfBits(ByVal hEmf As IntPtr, ByVal bufferSize As UInt32, ByVal buffer As Byte(), ByVal mappingMode As Integer, ByVal flags As EmfToWmfBitsFlags) As UInt32
End Function


End Class

I am not quite sure if the code is fullly correct. I've tested it with several pictures and it worked. But there's no guarantee that the above code is completely free of bugs.

Bye
Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top