Click to See Complete Forum and Search --> : Trying to speed up process of loading bitmap


506
April 1st, 2008, 05:39 PM
Hello: I'm working with the drawing class and am having big time issues with slowness when using GetPixel and SetPixel. I found some great script online that I'm trying to work with:( http://www.codeproject.com/KB/vb/FastPixel.aspx?msg=2489348#xx2489348xx) and it basically locks up the bitmap and stores the pixels into an array to work with.

When using it, it works for some of my bitmaps...but bombs for others. this is the error I get:

Index was outside the boundws of the array(see ''it bombs on this line: ' to see exact line below).

I understand that the array isn't big enough but i don't understand why it works for some of my bitmaps and not others.
When I save my bitmaps, I save them as 24 bit rgb color 300 dpi. I don't know much about resolutions and what format is best to save them as.

Has anyone here has experience working with bitmaps and drawing class? I could sure use any dirrection..here.

thanks,
Proctor

Public Function GetPixel(ByVal x As Integer, ByVal y As Integer) As Color
If Not Me.locked Then
Throw New Exception("Bitmap not locked.")
Return Nothing
End If

If Me.IsAlphaBitmap Then
Dim index As Integer = ((y * Me.Width + x) * 4)
Dim b As Integer = Me.rgbValues(index)
Dim g As Integer = Me.rgbValues(index + 1)
Dim r As Integer = Me.rgbValues(index + 2)
Dim a As Integer = Me.rgbValues(index + 3)
Return Color.FromArgb(a, r, g, b)
Else
Dim index As Integer = ((y * Me.Width + x) * 3)
'it bombs on this line: Dim b As Integer = Me.rgbValues(index)
Dim g As Integer = Me.rgbValues(index + 1)
Dim r As Integer = Me.rgbValues(index + 2)
Return Color.FromArgb(r, g, b)
End If
End Function

HanneSThEGreaT
April 2nd, 2008, 07:12 AM
Don't know if this would help, but this is what I use to GrayScale one of my images in a project.
As you can see, it loops through the image horizontally and vertically, while doing that, it determines the current pixel's colour, and modifies it, then, set the new pixel colour.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OldBmp As New Bitmap(PictureBox1.Image)
Dim PicX As Integer
Dim PicY As Integer
Dim Oldclr As Integer

For PicX = 0 To OldBmp.Width - 1
For PicY = 0 To OldBmp.Height - 1
Oldclr = (CInt(OldBmp.GetPixel(PicX, PicY).R) + _
OldBmp.GetPixel(PicX, PicY).G + _
OldBmp.GetPixel(PicX, PicY).B) \ 3

OldBmp.SetPixel(PicX, PicY, Color.FromArgb(Oldclr, Oldclr, Oldclr))

Next PicY
Next PicX

PictureBox1.Image = OldBmp

End Sub