元帅的问题 [英] Problem with Marshal.Copy

查看:232
本文介绍了元帅的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我已经发布了此信息,但没有得到任何回应,因此我将其再次发布以供您参考


Previously I have posted this but got no response hence I am posting it again for your kind reference


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




上面的简单代码让我在marshal.copy
看到了一个错误




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



我无法确定出了什么问题:(
请帮我...
我也尝试将选项JIT设置为off,但这不起作用..
我也尝试安装.net v1.1,但也失败了.

我现在很无助,陷入困境..请帮助:((

[edit]更改标题以描述问题,删除了Textspeak,突出显示. -OriginalGriff [/edit]



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]

推荐答案

我将为您提供有效的代码,以便您查看自己在做什么错. Nishant Sivakumar指出了错误所在的位置,因此请看一下此示例并尝试更正您的代码.
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


这篇关于元帅的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆