Understanding NET Framework Functionality

While this book isn't about general C# or Visual Basic programming, it's important to remember that the .NET Framework does provide the functionality required to display basic graphics. For example, you can embed a bitmap within your application and display it on screen as needed. You'll find such an application in the \Chapter 02\C#\NETBitmap and the \Chapter 02\VB\NETBitmap folders of the CD. Here's the basic code needed to perform the task.

private void btnTest_Click(object sender, System.EventArgs e) {

// Retrieve an embedded bitmap from the current assembly. Assembly Asm = Assembly.GetExecutingAssembly();

Stream Strm = Asm.GetManifestResourceStream("NETBitmap.Main.bmp"); Bitmap Temp = new Bitmap(Strm);

// Display the bitmap on screen. pbMain.Image = (Image)Temp;

As you can see, this example relies on reflection to get the job done. You must set the Build Action property of the bitmap or other resource to Embedded Resource to use this technique. Figure 2.7 shows the output from this example.

igure 2.7: Displaying an embedded bitmap within your .NET application is fine for the managed environment.

The resulting bitmap works fine within the managed environment but won't work within the Win32 API, which means you have to use a different technique when making a Win32 API call. The Win32 API doesn't understand the managed resources created by reflection. Fortunately, you can use the special GetHbitmap() call to answer many Win32 API call needs.

The hBitmap this call returns is Windows compatible. You must make a call to the Win32 API DeleteObject() function to deallocate the handle when you finish using it. Here's the declaration for the DeleteObject() function.


public static extern int DeleteObject(IntPtr hObject);

Was this article helpful?

0 0

Post a comment