Я хотел бы сравнить аналогичные изображения более быстрее, используя метод блокировки следующим образом < /p>
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class CompareImages {
public static void Main ( String[] args ) {
Bitmap bm1 = new Bitmap ( "PB270029.JPG" );
Console.WriteLine ( bm1.PixelFormat.ToString() );
int width = bm1.Width;
int height = bm1.Height;
Console.WriteLine ( "width = " + width + " height = " + height );
Rectangle rect1 = new Rectangle ( 0, 0, width, height );
BitmapData bm1Data = bm1.LockBits ( rect1, ImageLockMode.ReadOnly, bm1.PixelFormat );
Console.WriteLine ( "stride = " + bm1Data.Stride );
IntPtr bm1Ptr = bm1Data.Scan0;
int bytes = Math.Abs(bm1Data.Stride) * height;
Console.WriteLine ( "bytes = " + bytes );
byte[] rgbValues1 = new byte [ bytes ];
Marshal.Copy ( bm1Ptr, rgbValues1, 0, bytes );
Console.WriteLine ( "After 1st Marshal.Copy ..." );
Bitmap bm2 = new Bitmap ( "PA050164.JPG" );
Rectangle rect2 = new Rectangle ( 0, 0, bm2.Width, bm2.Height );
BitmapData bm2Data = bm2.LockBits ( rect2, ImageLockMode.ReadOnly, bm2.PixelFormat );
IntPtr bm2Ptr = bm2Data.Scan0;
byte[] rgbValues2 = new byte [ Math.Abs(bm2Data.Stride) * bm2.Height ];
Marshal.Copy ( bm2Ptr, rgbValues2, 0, rgbValues2.Length );
}
}
< /code>
Но во время второго маршала.copy возникает AccessViolationException: < /p>
C:\CompareImages>csc CompareImages.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
C:\CompareImages>CompareImages.exe
Format24bppRgb
width = 3648 height = 2736
stride = 10944
bytes = 29942784
After 1st Marshal.Copy ...
Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object
destination, Int32 startIndex, Int32 length)
at CompareImages.Main(String[] args)
< /code>
Что не так в моей программе?>
Подробнее здесь: https://stackoverflow.com/questions/481 ... g-lockbits
AccessViolation при использовании блокировки ⇐ C#
Место общения программистов C#
-
Anonymous
1753564831
Anonymous
Я хотел бы сравнить аналогичные изображения более быстрее, используя метод блокировки следующим образом < /p>
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class CompareImages {
public static void Main ( String[] args ) {
Bitmap bm1 = new Bitmap ( "PB270029.JPG" );
Console.WriteLine ( bm1.PixelFormat.ToString() );
int width = bm1.Width;
int height = bm1.Height;
Console.WriteLine ( "width = " + width + " height = " + height );
Rectangle rect1 = new Rectangle ( 0, 0, width, height );
BitmapData bm1Data = bm1.LockBits ( rect1, ImageLockMode.ReadOnly, bm1.PixelFormat );
Console.WriteLine ( "stride = " + bm1Data.Stride );
IntPtr bm1Ptr = bm1Data.Scan0;
int bytes = Math.Abs(bm1Data.Stride) * height;
Console.WriteLine ( "bytes = " + bytes );
byte[] rgbValues1 = new byte [ bytes ];
Marshal.Copy ( bm1Ptr, rgbValues1, 0, bytes );
Console.WriteLine ( "After 1st Marshal.Copy ..." );
Bitmap bm2 = new Bitmap ( "PA050164.JPG" );
Rectangle rect2 = new Rectangle ( 0, 0, bm2.Width, bm2.Height );
BitmapData bm2Data = bm2.LockBits ( rect2, ImageLockMode.ReadOnly, bm2.PixelFormat );
IntPtr bm2Ptr = bm2Data.Scan0;
byte[] rgbValues2 = new byte [ Math.Abs(bm2Data.Stride) * bm2.Height ];
Marshal.Copy ( bm2Ptr, rgbValues2, 0, rgbValues2.Length );
}
}
< /code>
Но во время второго маршала.copy возникает AccessViolationException: < /p>
C:\CompareImages>csc CompareImages.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
C:\CompareImages>CompareImages.exe
Format24bppRgb
width = 3648 height = 2736
stride = 10944
bytes = 29942784
After 1st Marshal.Copy ...
Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object
destination, Int32 startIndex, Int32 length)
at CompareImages.Main(String[] args)
< /code>
Что не так в моей программе?>
Подробнее здесь: [url]https://stackoverflow.com/questions/4816956/accessviolation-when-using-lockbits[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия