Click here to Skip to main content
15,889,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Previously I have posted this but got no response hence I am posting it again for your kind reference


VB
Dim MyImage As Bitmap = CType(Image.FromFile("C:\Alien 2.bmp"), Bitmap)
        Dim rectangle As New Rectangle(0, 0, MyImage.Width, MyImage.Height)
        Dim myImageData As System.Drawing.Imaging.BitmapData
        myImageData = MyImage.LockBits(rectangle, Imaging.ImageLockMode.ReadWrite, MyImage.PixelFormat)
        Dim bytes As Integer = myImageData.Stride * MyImage.Height
        Dim iwav(bytes - 1) As Short
        Dim ptr As IntPtr = myImageData.Scan0
        marshal.Copy(ptr, iwav, 0, bytes) 'ERROR



The above simple peice of code shows me a error at marshal.copy
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
A first chance exception of type 'System.AccessViolationException' occurred in mscorlib.dll


I cannot find out what has gone wrong :(
Please Help Me...
I have also tried setting option JIT to off but that doesnot work..
I have also tried installing .net v1.1 but that also fails.

I am now helpless and stuck.. Please Help :((

[edit]Title changed to describe problem, Textspeak removed, highlighting. - OriginalGriff[/edit]
Posted
Updated 16-Oct-10 22:12pm
v2
Comments
Sandeep Mewara 17-Oct-10 1:42am    
I remember reading this question yesterday and it was answered too! But it looks like, you have deleted that and reposted it. Why so?
Be Yourself 17-Oct-10 8:10am    
Sorry !!!
I have posted it again because I was struck in the middle and was not able to do any progress as the issue was not solved.

1 solution

I will provide you with working code so you can see what you are doing wrong. Nishant Sivakumar pointed to the location where the error is, so look at this example and try to correct your code.
Public g_RowSizeBytes As Integer
Public rgbValues() As Byte
Private bmpdata As Imaging.BitmapData
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
02
03         Dim bmp As New System.Drawing.Bitmap(picViewer1.Image)'IMAGE FROM PICTURE BOX...
04         Call LockBitmap(bmp)      'LOCKING BITMAP
06
07         Dim RedValue As Int32
08         Dim GreenValue As Int32
09         Dim BlueValue As Int32
10         Dim l As Integer = 0       ' Pixel position
11         For x = 0 To bmp.Width - 1    'Pixels start with 0 so we need Width - 1
12             For y = 0 To bmp.Height - 1  'Pixels start with 0 so we need Height - 1
13
14                 l = ((bmp.Width * 3 * y) + (x * 3)) ' pixel is made of 3 parts (RGB colors)just to understand ...
15                 RedValue = rgbValues(l)
16                 GreenValue = rgbValues(l + 1)
17                 BlueValue = rgbValues(l + 2)
18                 If RedValue > 250 AndAlso GreenValue > 250 AndAlso BlueValue > 250 Then
19                 Msgbox("Pixel is of a white color...")20                 ElseIf RedValue < 5 AndAlso GreenValue < 5 AndAlso BlueValue < 5 Then
21                  Msgbox("Pixel is of a black color..."
22                  Else
23                 Msgbox("Pixel is not white or black ...")
24                 End If
25             Next
26         Next
27          Call UnlockBitmap(bmp)      'UNLOCKING BITMAP
29
30         picViewer1.Image = (bmp)
31         picViewer1.Refresh()
32
33 End Sub
34
35 Private Sub LockBitmap(ByVal bmp As Bitmap)
36         Dim bounds As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height)
37         bmpdata = bmp.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)
38         g_RowSizeBytes = bmpdata.Stride
39         Dim total_size As Integer = bmpdata.Stride * bmpdata.Height
40         ReDim rgbValues(total_size)
41         Marshal.Copy(bmpdata.Scan0, rgbValues, 0, total_size)
42     End Sub
43
44     Private Sub UnlockBitmap(ByVal bmp As Bitmap)
45         Dim total_size As Integer = bmpdata.Stride * bmpdata.Height
46         Marshal.Copy(rgbValues, 0, bmpdata.Scan0, total_size)
47         bmp.UnlockBits(bmpdata)
48         rgbValues = Nothing
49         bmpdata = Nothing
50     End Sub
 
Share this answer
 
v2
Comments
Be Yourself 18-Oct-10 2:38am    
Thanks !!
But you have posted the same code with some different implementations to show the colour.
But my problem is with marshal.copy not with the programming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900