Click here to Skip to main content
16,018,637 members
Home / Discussions / C#
   

C#

 
GeneralRe: HELP Pin
Member 1094467922-Jul-14 4:07
Member 1094467922-Jul-14 4:07 
GeneralRe: HELP Pin
Ravi Bhavnani22-Jul-14 4:16
professionalRavi Bhavnani22-Jul-14 4:16 
AnswerRe: HELP Pin
Ron Nicholson22-Jul-14 4:08
professionalRon Nicholson22-Jul-14 4:08 
GeneralRe: HELP Pin
Richard MacCutchan22-Jul-14 5:04
mveRichard MacCutchan22-Jul-14 5:04 
GeneralRe: HELP Pin
OriginalGriff22-Jul-14 6:19
mveOriginalGriff22-Jul-14 6:19 
AnswerRe: HELP Pin
Rob Philpott22-Jul-14 5:25
Rob Philpott22-Jul-14 5:25 
GeneralRe: HELP Pin
Eddy Vluggen22-Jul-14 5:53
professionalEddy Vluggen22-Jul-14 5:53 
Questionbitmap lockbits/unlockbits Pin
V.22-Jul-14 2:48
professionalV.22-Jul-14 2:48 
I have a small prototype that allows a user to highlight a rectangle on an image. The program will analyze this highlighted portion and change the pixels properties of the pixels. Above a certain treshold it will set it to white, below the treshold the pixel is set to orange.

This stuff works using the SetPixel/GetPixel method, but I read that performance is better with the LockBits/UnlockBits method.
So I tried the following:
C#
System.Drawing.Imaging.BitmapData bmpdata = tmpbmp.LockBits(tmprect, System.Drawing.Imaging.ImageLockMode.ReadWrite, tmpbmp.PixelFormat);
IntPtr pointer = bmpdata.Scan0;
//int nrofbytes = Math.Abs(bmpdata.Stride) * bmpdata.Height;  //stride = scan width
int nrofbytes = bmpdata.Width * bmpdata.Height;  //stride = scan width
byte [] bytes = new byte[nrofbytes];
	
System.Runtime.InteropServices.Marshal.Copy(pointer, bytes, 0, nrofbytes);
System.Drawing.Color color_white = System.Drawing.Color.White;
System.Drawing.Color color_orange = System.Drawing.Color.Orange;
long count = 0;
for(int i = 0; i < bytes.Length-3; i+=3){
	if(bytes[i] > 235 && bytes[i+1] > 235 && bytes[i+2] > 235){
		bytes[i] = color_white.R;
		bytes[i+1] = color_white.G;
		bytes[i+2] = color_white.B;
	}											//end if
	else{
		count++;
		bytes[i] = color_orange.R;
		bytes[i+1] = color_orange.G;
		bytes[i+2] = color_orange.B;
	}											//end else
}												//end for
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, pointer, nrofbytes);
tmpbmp.UnlockBits(bmpdata);


This doesn't work in the sense that the highlighted part is:
1. not white/orange, but rather white/gray
2. it changes entire rows of the image instead of the rectangle portion I passed.

for 1. it might be some sort of index offset, I guess.
I'm pretty sure the culprit for 2. is this line:
C#
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, pointer, nrofbytes);


but I'm not sure why or how to solve it.
Anyone have any experience with this?

Many thanks.
V.

(MQOTD rules and previous solutions)

AnswerRe: bitmap lockbits/unlockbits Pin
Rob Philpott22-Jul-14 3:12
Rob Philpott22-Jul-14 3:12 
GeneralRe: bitmap lockbits/unlockbits Pin
V.22-Jul-14 3:32
professionalV.22-Jul-14 3:32 
AnswerRe: bitmap lockbits/unlockbits Pin
Kornfeld Eliyahu Peter22-Jul-14 3:45
professionalKornfeld Eliyahu Peter22-Jul-14 3:45 
GeneralRe: bitmap lockbits/unlockbits Pin
V.22-Jul-14 22:29
professionalV.22-Jul-14 22:29 
QuestionRe: bitmap lockbits/unlockbits Pin
Kornfeld Eliyahu Peter22-Jul-14 22:44
professionalKornfeld Eliyahu Peter22-Jul-14 22:44 
AnswerRe: bitmap lockbits/unlockbits Pin
V.22-Jul-14 22:51
professionalV.22-Jul-14 22:51 
AnswerRe: bitmap lockbits/unlockbits Pin
Kornfeld Eliyahu Peter22-Jul-14 22:55
professionalKornfeld Eliyahu Peter22-Jul-14 22:55 
GeneralRe: bitmap lockbits/unlockbits Pin
V.22-Jul-14 23:10
professionalV.22-Jul-14 23:10 
AnswerRe: bitmap lockbits/unlockbits Pin
Kornfeld Eliyahu Peter22-Jul-14 23:21
professionalKornfeld Eliyahu Peter22-Jul-14 23:21 
GeneralRe: bitmap lockbits/unlockbits Pin
V.23-Jul-14 2:05
professionalV.23-Jul-14 2:05 
AnswerRe: bitmap lockbits/unlockbits Pin
Kornfeld Eliyahu Peter23-Jul-14 2:56
professionalKornfeld Eliyahu Peter23-Jul-14 2:56 
AnswerRe: bitmap lockbits/unlockbits Pin
Kornfeld Eliyahu Peter22-Jul-14 3:24
professionalKornfeld Eliyahu Peter22-Jul-14 3:24 
GeneralRe: bitmap lockbits/unlockbits Pin
V.22-Jul-14 19:55
professionalV.22-Jul-14 19:55 
AnswerRe: bitmap lockbits/unlockbits Pin
Kornfeld Eliyahu Peter22-Jul-14 20:06
professionalKornfeld Eliyahu Peter22-Jul-14 20:06 
GeneralRe: bitmap lockbits/unlockbits Pin
V.22-Jul-14 20:13
professionalV.22-Jul-14 20:13 
QuestionClasses Pin
AnilUppala22-Jul-14 1:52
AnilUppala22-Jul-14 1:52 
AnswerRe: Classes Pin
Ravi Bhavnani22-Jul-14 2:06
professionalRavi Bhavnani22-Jul-14 2:06 

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.