Can someone please convert this C# to visual basic?

Latin4567

New member
Joined
Sep 18, 2005
Messages
3
Location
US, Connecticut
Programming Experience
1-3
Ok, I have tried for the past 24 hours to convert this C# code to visual basic without sucess. Could someone please plesae please please convert this to visual basic and post it?


Visual C# Code:
VB.NET:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;



// class that exposes needed win32 gdi functions.
class Win32
{
	public enum Bool
	{
		False= 0,
		True
	};


	[StructLayout(LayoutKind.Sequential)]
	public struct Point
	{
		public Int32 x;
		public Int32 y;

		public Point(Int32 x, Int32 y) { this.x= x; this.y= y; }
	}


	[StructLayout(LayoutKind.Sequential)]
	public struct Size {
		public Int32 cx;
		public Int32 cy;

		public Size(Int32 cx, Int32 cy) { this.cx= cx; this.cy= cy; }
	}


	[StructLayout(LayoutKind.Sequential, Pack=1)]
	struct ARGB
	{
		public byte Blue;
		public byte Green;
		public byte Red;
		public byte Alpha;
	}


	[StructLayout(LayoutKind.Sequential, Pack=1)]
	public struct BLENDFUNCTION
	{
		public byte BlendOp;
		public byte BlendFlags;
		public byte SourceConstantAlpha;
		public byte AlphaFormat;
	}


	public const Int32 ULW_COLORKEY = 0x00000001;
	public const Int32 ULW_ALPHA	= 0x00000002;
	public const Int32 ULW_OPAQUE   = 0x00000004;

	public const byte AC_SRC_OVER  = 0x00;
	public const byte AC_SRC_ALPHA = 0x01;


	[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

	[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern IntPtr GetDC(IntPtr hWnd);

	[DllImport("user32.dll", ExactSpelling=true)]
	public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

	[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

	[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern Bool DeleteDC(IntPtr hdc);

	[DllImport("gdi32.dll", ExactSpelling=true)]
	public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

	[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern Bool DeleteObject(IntPtr hObject);
}



/// <para>Your PerPixel form should inherit this class</para>
/// <author><name>Rui Godinho Lopes</name><email>rui@ruilopes.com</email></author>
class PerPixelAlphaForm : Form
{
	public PerPixelAlphaForm()
	{
		// This form should not have a border or else Windows will clip it.
		FormBorderStyle = FormBorderStyle.None;
	}


	/// <para>Changes the current bitmap.</para>
	public void SetBitmap(Bitmap bitmap)
	{
		SetBitmap(bitmap, 255);
	}


	/// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
	public void SetBitmap(Bitmap bitmap, byte opacity)
	{
		if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
			throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

		// The ideia of this is very simple,
		// 1. Create a compatible DC with screen;
		// 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
		// 3. Call the UpdateLayeredWindow.

		IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
		IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
		IntPtr hBitmap = IntPtr.Zero;
		IntPtr oldBitmap = IntPtr.Zero;

		try {
			hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
			oldBitmap = Win32.SelectObject(memDc, hBitmap);

			Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
			Win32.Point pointSource = new Win32.Point(0, 0);
			Win32.Point topPos = new Win32.Point(Left, Top);
			Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
		    blend.BlendOp			 = Win32.AC_SRC_OVER;
		    blend.BlendFlags		  = 0;
			blend.SourceConstantAlpha = opacity;
		    blend.AlphaFormat		 = Win32.AC_SRC_ALPHA;

		    Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
		}
		finally {
			Win32.ReleaseDC(IntPtr.Zero, screenDc);
			if (hBitmap != IntPtr.Zero) {
				Win32.SelectObject(memDc, oldBitmap);
		    	//Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
				Win32.DeleteObject(hBitmap);
			}
			Win32.DeleteDC(memDc);
		}
	}


	protected override CreateParams CreateParams
	{
		get {
			CreateParams cp = base.CreateParams;
			cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
			return cp;
		}
	}
}

Thank you sooooooo much to the person(s) that convert it. Any help at all is highly appreciated
 
You just missed one "}". I converted this using:
http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx
VB.NET:
 using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;



// class that exposes needed win32 gdi functions.
class Win32
{
	public enum Bool
	{
		False= 0,
		True
	};


	[StructLayout(LayoutKind.Sequential)]
	public struct Point
	{
		public Int32 x;
		public Int32 y;

		public Point(Int32 x, Int32 y) { this.x= x; this.y= y; }
	}


	[StructLayout(LayoutKind.Sequential)]
	public struct Size {
		public Int32 cx;
		public Int32 cy;

		public Size(Int32 cx, Int32 cy) { this.cx= cx; this.cy= cy; }
	}


	[StructLayout(LayoutKind.Sequential, Pack=1)]
	struct ARGB
	{
		public byte Blue;
		public byte Green;
		public byte Red;
		public byte Alpha;
	}


	[StructLayout(LayoutKind.Sequential, Pack=1)]
	public struct BLENDFUNCTION
	{
		public byte BlendOp;
		public byte BlendFlags;
		public byte SourceConstantAlpha;
		public byte AlphaFormat;
	}


	public const Int32 ULW_COLORKEY = 0x00000001;
	public const Int32 ULW_ALPHA	= 0x00000002;
	public const Int32 ULW_OPAQUE   = 0x00000004;

	public const byte AC_SRC_OVER  = 0x00;
	public const byte AC_SRC_ALPHA = 0x01;


	[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

	[DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern IntPtr GetDC(IntPtr hWnd);

	[DllImport("user32.dll", ExactSpelling=true)]
	public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

	[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

	[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern Bool DeleteDC(IntPtr hdc);

	[DllImport("gdi32.dll", ExactSpelling=true)]
	public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

	[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
	public static extern Bool DeleteObject(IntPtr hObject);
}



/// <para>Your PerPixel form should inherit this class</para>
/// <author><name>Rui Godinho Lopes</name><email>rui@ruilopes.com</email></author>
class PerPixelAlphaForm : Form
{
	public PerPixelAlphaForm()
	{
		// This form should not have a border or else Windows will clip it.
		FormBorderStyle = FormBorderStyle.None;
	}


	/// <para>Changes the current bitmap.</para>
	public void SetBitmap(Bitmap bitmap)
	{
		SetBitmap(bitmap, 255);
	}


	/// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
	public void SetBitmap(Bitmap bitmap, byte opacity)
	{
		if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
			throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

		// The ideia of this is very simple,
		// 1. Create a compatible DC with screen;
		// 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
		// 3. Call the UpdateLayeredWindow.

		IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
		IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
		IntPtr hBitmap = IntPtr.Zero;
		IntPtr oldBitmap = IntPtr.Zero;

		try {
			hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
			oldBitmap = Win32.SelectObject(memDc, hBitmap);

			Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
			Win32.Point pointSource = new Win32.Point(0, 0);
			Win32.Point topPos = new Win32.Point(Left, Top);
			Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
		    blend.BlendOp			 = Win32.AC_SRC_OVER;
		    blend.BlendFlags		  = 0;
			blend.SourceConstantAlpha = opacity;
		    blend.AlphaFormat		 = Win32.AC_SRC_ALPHA;

		    Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
		}
		finally {
			Win32.ReleaseDC(IntPtr.Zero, screenDc);
			if (hBitmap != IntPtr.Zero) {
				Win32.SelectObject(memDc, oldBitmap);
		    	//Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
				Win32.DeleteObject(hBitmap);
			}
			Win32.DeleteDC(memDc);
		}
	}


	protected override CreateParams CreateParams
	{
		get {
			CreateParams cp = base.CreateParams;
			cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
			return cp;
		}
	}
 
VB.NET:
Imports System 
Imports System.Drawing 
Imports System.Drawing.Imaging 
Imports System.Windows.Forms 
Imports System.Runtime.InteropServices 

Friend Class Win32 

  Public Enum Bool 
    Private False As  = 0 
    Private True As  
  End Enum  

  <StructLayout(LayoutKind.Sequential)> _ 
  Public Structure Point 
    Public x As Int32 
    Public y As Int32 

    Public Sub New(ByVal x As Int32, ByVal y As Int32) 
 	 Me.x = x 
 	 Me.y = y 
    End Sub 
  End Structure  

  <StructLayout(LayoutKind.Sequential)> _ 
  Public Structure Size 
    Public cx As Int32 
    Public cy As Int32 

    Public Sub New(ByVal cx As Int32, ByVal cy As Int32) 
 	 Me.cx = cx 
 	 Me.cy = cy 
    End Sub 
  End Structure  

  <StructLayout(LayoutKind.SequentialPack=1)> _ 
  Structure ARGB 
    Public Blue As Byte 
    Public Green As Byte 
    Public Red As Byte 
    Public Alpha As Byte 
  End Structure  

  <StructLayout(LayoutKind.SequentialPack=1)> _ 
  Public Structure BLENDFUNCTION 
    Public BlendOp As Byte 
    Public BlendFlags As Byte 
    Public SourceConstantAlpha As Byte 
    Public AlphaFormat As Byte 
  End Structure  
  Public Const ULW_COLORKEY As Int32 = 1 
  Public Const ULW_ALPHA As Int32 = 2 
  Public Const ULW_OPAQUE As Int32 = 4 
  Public Const AC_SRC_OVER As Byte = 0 
  Public Const AC_SRC_ALPHA As Byte = 1 

  <DllImport("user32.dll"ExactSpelling=True, SetLastError=True)>  
  Public Shared Function UpdateLayeredWindow(ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, ByRef pptDst As Point, ByRef psize As Size, ByVal hdcSrc As IntPtr, ByRef pprSrc As Point, ByVal crKey As Int32, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Int32) As Bool 
  End Function 

  <DllImport("user32.dll"ExactSpelling=True, SetLastError=True)>  
  Public Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr 
  End Function 

  <DllImport("user32.dll"ExactSpelling=True)>  
  Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer 
  End Function 

  <DllImport("gdi32.dll"ExactSpelling=True, SetLastError=True)>  
  Public Shared Function CreateCompatibleDC(ByVal hDC As IntPtr) As IntPtr 
  End Function 

  <DllImport("gdi32.dll"ExactSpelling=True, SetLastError=True)>  
  Public Shared Function DeleteDC(ByVal hdc As IntPtr) As Bool 
  End Function 

  <DllImport("gdi32.dll"ExactSpelling=True)>  
  Public Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr 
  End Function 

  <DllImport("gdi32.dll"ExactSpelling=True, SetLastError=True)>  
  Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Bool 
  End Function 
End Class  

Class PerPixelAlphaForm 
Inherits Form 

  Public Sub New() 
    FormBorderStyle = FormBorderStyle.None 
  End Sub 

  Public Sub SetBitmap(ByVal bitmap As Bitmap) 
    SetBitmap(bitmap, 255) 
  End Sub 

  Public Sub SetBitmap(ByVal bitmap As Bitmap, ByVal opacity As Byte) 
    If Not (bitmap.PixelFormat = PixelFormat.Format32bppArgb) Then 
 	 Throw New ApplicationException("The bitmap must be 32ppp with alpha-channel.") 
    End If 
    Dim screenDc As IntPtr = Win32.GetDC(IntPtr.Zero) 
    Dim memDc As IntPtr = Win32.CreateCompatibleDC(screenDc) 
    Dim hBitmap As IntPtr = IntPtr.Zero 
    Dim oldBitmap As IntPtr = IntPtr.Zero 
    Try 
 	 hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)) 
 	 oldBitmap = Win32.SelectObject(memDc, hBitmap) 
 	 Dim size As Win32.Size = New Win32.Size(bitmap.Width, bitmap.Height) 
 	 Dim pointSource As Win32.Point = New Win32.Point(0, 0) 
 	 Dim topPos As Win32.Point = New Win32.Point(Left, Top) 
 	 Dim blend As Win32.BLENDFUNCTION = New Win32.BLENDFUNCTION 
 	 blend.BlendOp = Win32.AC_SRC_OVER 
 	 blend.BlendFlags = 0 
 	 blend.SourceConstantAlpha = opacity 
 	 blend.AlphaFormat = Win32.AC_SRC_ALPHA 
 	 Win32.UpdateLayeredWindow(Handle, screenDc, topPos, size, memDc, pointSource, 0, blend, Win32.ULW_ALPHA) 
    Finally 
 	 Win32.ReleaseDC(IntPtr.Zero, screenDc) 
 	 If Not (hBitmap = IntPtr.Zero) Then 
 	   Win32.SelectObject(memDc, oldBitmap) 
 	   Win32.DeleteObject(hBitmap) 
 	 End If 
 	 Win32.DeleteDC(memDc) 
    End Try 
  End Sub 

  Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams 
    Get 
 	 Dim cp As CreateParams = MyBase.CreateParams 
 	 cp.ExStyle = cp.ExStyle Or (524288) 
 	 Return cp 
    End Get 
  End Property 
End Class
 
Back
Top