How to Create a BitMap Viewer in Visual Basic

Introduction: Welcome to a tutorial on how to create a BitMap photo viewer in Visual Basic. Steps of Creation: Step 1: First we want to create a form with a button to load and show the bitmap file, and a picturebox to display the bitmap. Step 2: Now, in the button click event we want to put this code:
  1. Try
  2. Dim fo As New OpenFileDialog
  3. fo.Filter = "BitMap Files|*.bmp"
  4. fo.FilterIndex = 1
  5. fo.ShowDialog()
  6. If (fo.FileName.ToLower().EndsWith(".bmp")) Then
  7. Dim image1 As Bitmap = New Bitmap(fo.FileName, True)
  8. Dim x, y As Integer
  9. For x = 0 To image1.Width - 1
  10. For y = 0 To image1.Height - 1
  11. Dim pixelColor As Color = image1.GetPixel(x, y)
  12. Dim newColor As Color = _
  13. Color.FromArgb(pixelColor.R, 0, 0)
  14. image1.SetPixel(x, y, newColor)
  15. Next
  16. Next
  17. PictureBox1.Image = image1
  18. Me.Width = image1.Width + 60
  19. PictureBox1.Width = image1.Width
  20. PictureBox1.Location = New Point(10, 50)
  21. PictureBox1.Height = image1.Height
  22. Me.Height = image1.Height + 50
  23. Else : MsgBox("That is not a BitMap file. Bitmap files end with "".bmp"".")
  24. End If
  25. Catch ex As ArgumentException
  26. MessageBox.Show(ex.ToString())
  27. End Try
We surround the entire block with try and catch to avoid errors being thrown and causing a program crash. First we let the user select their BitMap image file, we then create that bitmap and run through each pixel on the y axis for each pixel on the x axis and set the image1 (our bitmap variable) pixel location to the colour of the image pixel. Once we have loaded all the bitmap pixels and set our picturebox to the image, we set the width, height and locations of the appropriate controls to make the photo full view and size. Project Complete! That's it! Below is the full source code and download to the project files.
  1. Public Class Form1
  2.  
  3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4. Try
  5. Dim fo As New OpenFileDialog
  6. fo.Filter = "BitMap Files|*.bmp"
  7. fo.FilterIndex = 1
  8. fo.ShowDialog()
  9. If (fo.FileName.ToLower().EndsWith(".bmp")) Then
  10. Dim image1 As Bitmap = New Bitmap(fo.FileName, True)
  11. Dim x, y As Integer
  12. For x = 0 To image1.Width - 1
  13. For y = 0 To image1.Height - 1
  14. Dim pixelColor As Color = image1.GetPixel(x, y)
  15. Dim newColor As Color = _
  16. Color.FromArgb(pixelColor.R, 0, 0)
  17. image1.SetPixel(x, y, newColor)
  18. Next
  19. Next
  20. PictureBox1.Image = image1
  21. Me.Width = image1.Width + 60
  22. PictureBox1.Width = image1.Width
  23. PictureBox1.Location = New Point(10, 50)
  24. PictureBox1.Height = image1.Height
  25. Me.Height = image1.Height + 50
  26. Else : MsgBox("That is not a BitMap file. Bitmap files end with "".bmp"".")
  27. End If
  28. Catch ex As ArgumentException
  29. MessageBox.Show(ex.ToString())
  30. End Try
  31. End Sub
  32. End Class

Add new comment