C# Conversion help needed.

R

rob

Would somebody be kind enough to convert this code to VB.Net for me?

private string GetRtfImage(Image _image) {

StringBuilder _rtf = null;

// Used to store the enhanced metafile
MemoryStream _stream = null;

// Used to create the metafile and draw the image
Graphics _graphics = null;

// The enhanced metafile
Metafile _metaFile = null;

// Handle to the device context used to create the metafile
IntPtr _hdc;

try {
_rtf = new StringBuilder();
_stream = new MemoryStream();

// Get a graphics context from the RichTextBox
using(_graphics = this.CreateGraphics()) {

// Get the device context from the graphics context
_hdc = _graphics.GetHdc();

// Create a new Enhanced Metafile from the device context
_metaFile = new Metafile(_stream, _hdc);

// Release the device context
_graphics.ReleaseHdc(_hdc);
}

// Get a graphics context from the Enhanced Metafile
using(_graphics = Graphics.FromImage(_metaFile)) {

// Draw the image on the Enhanced Metafile
_graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width,
_image.Height));

}

// Get the handle of the Enhanced Metafile
IntPtr _hEmf = _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.
uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

// Create an array to hold the bits
byte[] _buffer = new byte[_bufferSize];

// A call to EmfToWmfBits with a valid buffer copies the bits into
the
// buffer an returns the number of bits in the WMF.
uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer,
MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

// Append the bits to the RTF string
for(int i = 0; i < _buffer.Length; ++i) {
_rtf.Append(String.Format("{0:X2}", _buffer));
}

return _rtf.ToString();
}
finally {
if(_graphics != null)
_graphics.Dispose();
if(_metaFile != null)
_metaFile.Dispose();
if(_stream != null)
_stream.Close();
}
}



Thanks,
Rob Panosh
 
G

Guest

Our Instant VB C# to VB.NET converter produces this equivalent:

Private Function GetRtfImage(ByVal _image As Image) As String

Dim _rtf As StringBuilder = Nothing

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

' Used to create the metafile and draw the image
Dim _graphics As Graphics = Nothing

' The enhanced metafile
Dim _metaFile As Metafile = Nothing

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

Try
_rtf = New StringBuilder()
_stream = New MemoryStream()

' Get a graphics context from the RichTextBox
'INSTANT VB NOTE: The following 'using' block is replaced by its pre-VB.NET
2005 equivalent:
' using(_graphics = Me.CreateGraphics())
Try

' Get the device context from the
graphics context
_hdc = _graphics.GetHdc()

' Create a new Enhanced Metafile
from the device context
_metaFile = New Metafile(_stream,
_hdc)

' Release the device context
_graphics.ReleaseHdc(_hdc)
Finally
Dim disp As IDisposable = _graphics
disp.Dispose()
End Try
'INSTANT VB NOTE: End of the original C# 'using' block

' Get a graphics context from the Enhanced
Metafile
'INSTANT VB NOTE: The following 'using' block is replaced by its pre-VB.NET
2005 equivalent:
' using(_graphics =
Graphics.FromImage(_metaFile))
Try

' Draw the image on the Enhanced
Metafile
_graphics.DrawImage(_image, New
Rectangle(0, 0, _image.Width, _image.Height))

Finally
Dim disp As IDisposable = _graphics
disp.Dispose()
End Try
'INSTANT VB NOTE: End of the original C# 'using' block

' Get the handle of the Enhanced Metafile
Dim _hEmf As IntPtr =
_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.
Dim _bufferSize As System.UInt32 =
GdipEmfToWmfBits(_hEmf, 0, Nothing, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)

' Create an array to hold the bits
Dim _buffer As Byte() = New Byte(_bufferSize
- 1) {}

' A call to EmfToWmfBits with a valid buffer
copies the bits into
' buffer an returns the number of bits in
the WMF.
the System.UInt32 _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize,
_buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault)

' Append the bits to the RTF string
Dim i As Integer = 0
'ORIGINAL LINE: for(int i = 0; i < _buffer.Length; i += 1)
'INSTANT VB NOTE: This 'for' loop was translated to a VB 'Do While' loop:
Do While i < _buffer.Length
_rtf.Append(String.Format("{0:X2}",
_buffer(i)))
i += 1
Loop

Return _rtf.ToString()
Finally
If Not _graphics Is Nothing Then
_graphics.Dispose()
End If
If Not _metaFile Is Nothing Then
_metaFile.Dispose()
End If
If Not _stream Is Nothing Then
_stream.Close()
End If
End Try
End Function

HTH,
David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

rob said:
Would somebody be kind enough to convert this code to VB.Net for me?

private string GetRtfImage(Image _image) {

StringBuilder _rtf = null;

// Used to store the enhanced metafile
MemoryStream _stream = null;

// Used to create the metafile and draw the image
Graphics _graphics = null;

// The enhanced metafile
Metafile _metaFile = null;

// Handle to the device context used to create the metafile
IntPtr _hdc;

try {
_rtf = new StringBuilder();
_stream = new MemoryStream();

// Get a graphics context from the RichTextBox
using(_graphics = this.CreateGraphics()) {

// Get the device context from the graphics context
_hdc = _graphics.GetHdc();

// Create a new Enhanced Metafile from the device context
_metaFile = new Metafile(_stream, _hdc);

// Release the device context
_graphics.ReleaseHdc(_hdc);
}

// Get a graphics context from the Enhanced Metafile
using(_graphics = Graphics.FromImage(_metaFile)) {

// Draw the image on the Enhanced Metafile
_graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width,
_image.Height));

}

// Get the handle of the Enhanced Metafile
IntPtr _hEmf = _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.
uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

// Create an array to hold the bits
byte[] _buffer = new byte[_bufferSize];

// A call to EmfToWmfBits with a valid buffer copies the bits into
the
// buffer an returns the number of bits in the WMF.
uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer,
MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

// Append the bits to the RTF string
for(int i = 0; i < _buffer.Length; ++i) {
_rtf.Append(String.Format("{0:X2}", _buffer));
}

return _rtf.ToString();
}
finally {
if(_graphics != null)
_graphics.Dispose();
if(_metaFile != null)
_metaFile.Dispose();
if(_stream != null)
_stream.Close();
}
}



Thanks,
Rob Panosh
 

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