Click here to Skip to main content
15,868,164 members
Home / Discussions / C#
   

C#

 
AnswerRe: problem with using pointers in C# Pin
OriginalGriff12-Dec-12 5:06
mveOriginalGriff12-Dec-12 5:06 
AnswerRe: problem with using pointers in C# Pin
DaveyM6912-Dec-12 8:08
professionalDaveyM6912-Dec-12 8:08 
AnswerRe: problem with using pointers in C# Pin
trønderen12-Dec-12 10:57
trønderen12-Dec-12 10:57 
QuestionValue of label is not changing upon loading or changing the value of the combobox Pin
jon-8012-Dec-12 4:37
professionaljon-8012-Dec-12 4:37 
AnswerRe: Value of label is not changing upon loading or changing the value of the combobox Pin
Eddy Vluggen12-Dec-12 4:42
professionalEddy Vluggen12-Dec-12 4:42 
AnswerRe: Value of label is not changing upon loading or changing the value of the combobox Pin
Freak3012-Dec-12 4:52
Freak3012-Dec-12 4:52 
QuestionApply diff image on the original image c# Pin
Tridip Bhattacharjee12-Dec-12 0:22
professionalTridip Bhattacharjee12-Dec-12 0:22 
QuestionTwo image compare using win32 api c# Pin
Tridip Bhattacharjee12-Dec-12 0:21
professionalTridip Bhattacharjee12-Dec-12 0:21 
i have two images and i want to compare two image and want to get difference. i search google and found a link from where i copy paste the code for image comparison using win32 api. so this is the url http://blog.bobcravens.com/2009/04/create-a-remote-desktop-viewer-using-c-and-wcf/

here i am pasting the code.

private void button1_Click(object sender, EventArgs e)
{
Bitmap _prevBitmap = new Bitmap(@"d:\prev.jpg");
Bitmap _newBitmap = new Bitmap(@"d:\current.jpg");

Rectangle bounds = GetBoundingBoxForChanges(_prevBitmap, _newBitmap);
if (bounds == Rectangle.Empty)
{
}

Bitmap diff = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(diff);
g.DrawImage(_newBitmap, 0, 0, bounds, GraphicsUnit.Pixel);
g.Dispose();

// Set the current bitmap as the previous to prepare
// for the next screen capture.
//
diff.Save(@"d:\diff.bmp");

//return diff;
}

private Rectangle GetBoundingBoxForChanges(Bitmap _prevBitmap, Bitmap _newBitmap)
{
// The search algorithm starts by looking
// for the top and left bounds. The search
// starts in the upper-left corner and scans
// left to right and then top to bottom. It uses
// an adaptive approach on the pixels it
// searches. Another pass is looks for the
// lower and right bounds. The search starts
// in the lower-right corner and scans right
// to left and then bottom to top. Again, an
// adaptive approach on the search area is used.
//

// Note: The GetPixel member of the Bitmap class
// is too slow for this purpose. This is a good
// case of using unsafe code to access pointers
// to increase the speed.
//

// Validate the images are the same shape and type.
//
if (_prevBitmap.Width != _newBitmap.Width ||
_prevBitmap.Height != _newBitmap.Height ||
_prevBitmap.PixelFormat != _newBitmap.PixelFormat)
{
// Not the same shape...can't do the search.
//
return Rectangle.Empty;
}

// Init the search parameters.
//
int width = _newBitmap.Width;
int height = _newBitmap.Height;
int left = width;
int right = 0;
int top = height;
int bottom = 0;

BitmapData bmNewData = null;
BitmapData bmPrevData = null;
try
{
// Lock the bits into memory.
//
bmNewData = _newBitmap.LockBits(
new Rectangle(0, 0, _newBitmap.Width, _newBitmap.Height),
ImageLockMode.ReadOnly, _newBitmap.PixelFormat);
bmPrevData = _prevBitmap.LockBits(
new Rectangle(0, 0, _prevBitmap.Width, _prevBitmap.Height),
ImageLockMode.ReadOnly, _prevBitmap.PixelFormat);

// The images are ARGB (4 bytes)
//
int numBytesPerPixel = 4;

// Get the number of integers (4 bytes) in each row
// of the image.
//
int strideNew = bmNewData.Stride / numBytesPerPixel;
int stridePrev = bmPrevData.Stride / numBytesPerPixel;

// Get a pointer to the first pixel.
//
// Note: Another speed up implemented is that I don't
// need the ARGB elements. I am only trying to detect
// change. So this algorithm reads the 4 bytes as an
// integer and compares the two numbers.
//
System.IntPtr scanNew0 = bmNewData.Scan0;
System.IntPtr scanPrev0 = bmPrevData.Scan0;

// Enter the unsafe code.
//
unsafe
{
// Cast the safe pointers into unsafe pointers.
//
int* pNew = (int*)(void*)scanNew0;
int* pPrev = (int*)(void*)scanPrev0;

// First Pass - Find the left and top bounds
// of the minimum bounding rectangle. Adapt the
// number of pixels scanned from left to right so
// we only scan up to the current bound. We also
// initialize the bottom & right. This helps optimize
// the second pass.
//
// For all rows of pixels (top to bottom)
//
for (int y = 0; y < _newBitmap.Height; ++y)
{
// For pixels up to the current bound (left to right)
//
for (int x = 0; x < left; ++x)
{
// Use pointer arithmetic to index the
// next pixel in this row.
//
if ((pNew + x)[0] != (pPrev + x)[0])
{
// Found a change.
//
if (x < left)
{
left = x;
}
if (x > right)
{
right = x;
}
if (y < top)
{
top = y;
}
if (y > bottom)
{
bottom = y;
}
}
}

// Move the pointers to the next row.
//
pNew += strideNew;
pPrev += stridePrev;
}

// If we did not find any changed pixels
// then no need to do a second pass.
//
if (left != width)
{
// Second Pass - The first pass found at
// least one different pixel and has set
// the left & top bounds. In addition, the
// right & bottom bounds have been initialized.
// Adapt the number of pixels scanned from right
// to left so we only scan up to the current bound.
// In addition, there is no need to scan past
// the top bound.
//

// Set the pointers to the first element of the
// bottom row.
//
pNew = (int*)(void*)scanNew0;
pPrev = (int*)(void*)scanPrev0;
pNew += (_newBitmap.Height - 1) * strideNew;
pPrev += (_prevBitmap.Height - 1) * stridePrev;

// For each row (bottom to top)
//
for (int y = _newBitmap.Height - 1; y > top; y--)
{
// For each column (right to left)
//
for (int x = _newBitmap.Width - 1; x > right; x--)
{
// Use pointer arithmetic to index the
// next pixel in this row.
//
if ((pNew + x)[0] != (pPrev + x)[0])
{
// Found a change.
//
if (x > right)
{
right = x;
}
if (y > bottom)
{
bottom = y;
}
}
}

// Move up one row.
//
pNew -= strideNew;
pPrev -= stridePrev;
}
}
}
}
catch (Exception ex)
{
int xxx = 0;
}
finally
{
// Unlock the bits of the image.
//
if (bmNewData != null)
{
_newBitmap.UnlockBits(bmNewData);
}
if (bmPrevData != null)
{
_prevBitmap.UnlockBits(bmPrevData);
}
}

// Validate we found a bounding box. If not
// return an empty rectangle.
//
int diffImgWidth = right - left + 1;
int diffImgHeight = bottom - top + 1;
if (diffImgHeight < 0 || diffImgWidth < 0)
{
// Nothing changed
return Rectangle.Empty;
}

// Return the bounding box.
//
return new Rectangle(left, top, diffImgWidth, diffImgHeight);
}

when GetBoundingBoxForChanges() call then i am getting error and error message is Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

error occur at this code if ((pNew + x)[0] != (pPrev + x)[0])

so i am not being able to find out the reason. how to fix this error. please guide. thanks
tbhattacharjee

AnswerRe: Two image compare using win32 api c# Pin
Pete O'Hanlon12-Dec-12 1:01
subeditorPete O'Hanlon12-Dec-12 1:01 
QuestionCode does not appear to be called when program is run Pin
bikerben12-Dec-12 0:07
bikerben12-Dec-12 0:07 
AnswerRe: Code does not appear to be called when program is run Pin
Pete O'Hanlon12-Dec-12 1:06
subeditorPete O'Hanlon12-Dec-12 1:06 
GeneralRe: Code does not appear to be called when program is run Pin
bikerben12-Dec-12 1:23
bikerben12-Dec-12 1:23 
GeneralRe: Code does not appear to be called when program is run Pin
Pete O'Hanlon12-Dec-12 2:25
subeditorPete O'Hanlon12-Dec-12 2:25 
SuggestionRe: Code does not appear to be called when program is run Pin
Richard MacCutchan12-Dec-12 3:07
mveRichard MacCutchan12-Dec-12 3:07 
GeneralRe: Code does not appear to be called when program is run Pin
bikerben12-Dec-12 3:12
bikerben12-Dec-12 3:12 
GeneralRe: Code does not appear to be called when program is run Pin
Alan N12-Dec-12 3:07
Alan N12-Dec-12 3:07 
QuestionTFS and WIX Pin
Pascal Ganaye11-Dec-12 22:33
Pascal Ganaye11-Dec-12 22:33 
QuestionCanny edge detection in c# Pin
neha198711-Dec-12 22:11
neha198711-Dec-12 22:11 
AnswerRe: Canny edge detection in c# Pin
Simon_Whale11-Dec-12 22:23
Simon_Whale11-Dec-12 22:23 
AnswerRe: Canny edge detection in c# Pin
Pete O'Hanlon11-Dec-12 22:34
subeditorPete O'Hanlon11-Dec-12 22:34 
QuestionWCF Services in a multi threaded application results in Oracle Connection request Timed out error Pin
rajaron11-Dec-12 21:56
rajaron11-Dec-12 21:56 
AnswerRe: WCF Services in a multi threaded application results in Oracle Connection request Timed out error Pin
Eddy Vluggen12-Dec-12 3:05
professionalEddy Vluggen12-Dec-12 3:05 
GeneralRunning code as different user Pin
MacUseless11-Dec-12 21:35
MacUseless11-Dec-12 21:35 
SuggestionRe: Running code as different user Pin
Richard Deeming12-Dec-12 1:51
mveRichard Deeming12-Dec-12 1:51 
AnswerRe: Running code as different user Pin
Eddy Vluggen12-Dec-12 3:31
professionalEddy Vluggen12-Dec-12 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.