How to Create a BitMap Viewer in Visual Basic
Submitted by GeePee on Thursday, April 23, 2015 - 22:58.
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:
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.
- Try
- Dim fo As New OpenFileDialog
- fo.Filter = "BitMap Files|*.bmp"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If (fo.FileName.ToLower().EndsWith(".bmp")) Then
- Dim image1 As Bitmap = New Bitmap(fo.FileName, True)
- Dim x, y As Integer
- For x = 0 To image1.Width - 1
- For y = 0 To image1.Height - 1
- Dim pixelColor As Color = image1.GetPixel(x, y)
- Dim newColor As Color = _
- Color.FromArgb(pixelColor.R, 0, 0)
- image1.SetPixel(x, y, newColor)
- Next
- Next
- PictureBox1.Image = image1
- Me.Width = image1.Width + 60
- PictureBox1.Width = image1.Width
- PictureBox1.Location = New Point(10, 50)
- PictureBox1.Height = image1.Height
- Me.Height = image1.Height + 50
- Else : MsgBox("That is not a BitMap file. Bitmap files end with "".bmp"".")
- End If
- Catch ex As ArgumentException
- MessageBox.Show(ex.ToString())
- End Try
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Try
- Dim fo As New OpenFileDialog
- fo.Filter = "BitMap Files|*.bmp"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If (fo.FileName.ToLower().EndsWith(".bmp")) Then
- Dim image1 As Bitmap = New Bitmap(fo.FileName, True)
- Dim x, y As Integer
- For x = 0 To image1.Width - 1
- For y = 0 To image1.Height - 1
- Dim pixelColor As Color = image1.GetPixel(x, y)
- Dim newColor As Color = _
- Color.FromArgb(pixelColor.R, 0, 0)
- image1.SetPixel(x, y, newColor)
- Next
- Next
- PictureBox1.Image = image1
- Me.Width = image1.Width + 60
- PictureBox1.Width = image1.Width
- PictureBox1.Location = New Point(10, 50)
- PictureBox1.Height = image1.Height
- Me.Height = image1.Height + 50
- Else : MsgBox("That is not a BitMap file. Bitmap files end with "".bmp"".")
- End If
- Catch ex As ArgumentException
- MessageBox.Show(ex.ToString())
- End Try
- End Sub
- End Class
Add new comment
- 116 views