在鼠标移动时获得bmp的rgb值 [英] getting rgb value of a bmp on mouse move
问题描述
我希望使用aforge.net api在c sharp中获取鼠标移动事件中图像的rgb值。当移动鼠标图像时,相应地显示该点的rgb值。
I想要使用非托管图像和指针数据。我不必使用 getpixel()
method.kindly帮我。下面是我的代码。
提前感谢
private void Form1_MouseMove(object sender,MouseEventArgs e)
{
// loading image
Graphics g = CreateGraphics();
位图bmp =新位图(bmpimage54.bmp);
Rectangle r = new Rectangle(0,0,bmp.Width,bmp.Height);
g.DrawImage(bmp,r,r,GraphicsUnit.Pixel);
//在鼠标移动时获取像素值
Rectangle sourceRect = new Rectangle(0,0,bmp.Width,bmp.Height);
BitmapData imageData = bmp.LockBits(sourceRect,ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
UnmanagedImage unmanagedImage = new UnmanagedImage(imageData);
unsafe
{
byte * imgPtr =(byte *)imageData.Scan0.ToPointer();
byte * ptr;
for(int i = 0; i< imageData.Height; i ++)
{
for(int j = 0; j< imageData.Width; j ++)
{
imgPtr + = 3;
}
imgPtr + = imageData.Stride - imageData.Width * 3;
}
}
bmp.UnlockBits(imageData);
}
为什么你要申请循环?
一旦你知道了坐标,你可以使用索引即方括号来获得特定颜色的值
i已将图像加载到图片框中,然后使用鼠标移动图片框事件
private void pictureBox1_MouseMove( object sender,MouseEventArgs e)
{
Bitmap bmp = 新位图(pictureBox1.Image);
BitmapData bd = bmp.LockBits( new 矩形( 0 , 0 跨度>,bmp.Width,bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);
不安全
{
byte * ptr =(byte *)bd.Scan0;
int x = e.X * 3 ;
int y = e.Y * bd.Stride;
label1.Text = e.Location.ToString();
label2.Text = ptr [x + y] .ToString()+ , + ptr [x + y + 1 ]。ToString()+ , + ptr [x + y + 2 ]。ToString();
}
}
来自cordinates的
,计算x,y的值,然后x + y用于到达像素位置
x + y给出蓝色值
x + y + 1给出绿色值
x + y + 2给出红色值
谢谢你abdur Rehman。它真的解决了我的问题。
BitmapData bd;
private void detection_pictureBox_MouseMove(object sender,MouseEventArgs e)
{
if(detection_pictureBox.Image!= null)
{
unsafe
{
byte * ptr =(byte *)bd.Scan0;
int x = e.X * 3;
int y = e.Y * bd.Stride;
CurrPointerPos_lbl.Text = e.Location.ToString();
CurrPointerCol_lbl.Text = ptr [x + y] .ToString()+,+ ptr [x + y + 1] .ToString()+,+ ptr [x + y + 2] .ToString ();
}
}
}
private void detection_pictureBox_MouseEnter(object sender,EventArgs e)
{
if(detection_pictureBox。 Image!= null)
{
bd =(RunImgProcess.detectionImg).LockBits(new Rectangle(0,0,RunImgProcess.detectionImg.Width,RunImgProcess.detectionImg.Height),ImageLockMode.ReadOnly,RunImgProcess。 detectionImg.PixelFormat);
}
}
private void detection_pictureBox_MouseLeave(object sender,EventArgs e)
{
if(detection_pictureBox.Image!= null)
{
(RunImgProcess.detectionImg).UnlockBits(bd);
}
}
I want to get rgb value of the image on mouse move event in c sharp using aforge.net api.As when move mouse image then correspondingly it displays the rgb value of that point.
I want to do it using unmanaged image and pointer data. I dont have to usegetpixel()
method.kindly help me.below is my code.
thanks in advance
private void Form1_MouseMove(object sender, MouseEventArgs e) { // loading image Graphics g = CreateGraphics(); Bitmap bmp = new Bitmap("bmpimage54.bmp"); Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height); g.DrawImage(bmp, r, r, GraphicsUnit.Pixel); // getting pixel values on mouse move Rectangle sourceRect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData imageData = bmp.LockBits(sourceRect, ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb); UnmanagedImage unmanagedImage = new UnmanagedImage( imageData ); unsafe { byte* imgPtr = (byte*)imageData.Scan0.ToPointer(); byte* ptr; for (int i = 0; i < imageData.Height; i++) { for (int j = 0; j < imageData.Width; j++) { imgPtr += 3; } imgPtr += imageData.Stride - imageData.Width * 3; } } bmp.UnlockBits(imageData); }解决方案why u are applying loop?
once u know the co-ordinates, u can use indexing i.e square brackets to get the value of that particular color
i have loaded an image into picturebox, then using mouse move event of picture box
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { Bitmap bmp=new Bitmap(pictureBox1.Image); BitmapData bd=bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb); unsafe { byte* ptr = (byte*)bd.Scan0; int x = e.X * 3; int y = e.Y * bd.Stride; label1.Text = e.Location.ToString(); label2.Text = ptr[x + y].ToString() + "," + ptr[x + y + 1].ToString() + "," + ptr[x + y + 2].ToString(); } }
from cordinates, value of x,y is calculated, then x+y is used to reach the pixel location
x+y gives blue color value
x+y+1 gives green color value
x+y+2 gives red color value
thank u abdur Rehman. It really solved my problem.
BitmapData bd; private void detection_pictureBox_MouseMove(object sender, MouseEventArgs e) { if (detection_pictureBox.Image != null) { unsafe { byte* ptr = (byte*)bd.Scan0; int x = e.X * 3; int y = e.Y * bd.Stride; CurrPointerPos_lbl.Text = e.Location.ToString(); CurrPointerCol_lbl.Text = ptr[x + y].ToString() + "," + ptr[x + y + 1].ToString() + "," + ptr[x + y + 2].ToString(); } } } private void detection_pictureBox_MouseEnter(object sender, EventArgs e) { if (detection_pictureBox.Image != null) { bd = (RunImgProcess.detectionImg).LockBits(new Rectangle(0, 0, RunImgProcess.detectionImg.Width, RunImgProcess.detectionImg.Height), ImageLockMode.ReadOnly, RunImgProcess.detectionImg.PixelFormat); } } private void detection_pictureBox_MouseLeave(object sender, EventArgs e) { if (detection_pictureBox.Image != null) { (RunImgProcess.detectionImg).UnlockBits(bd); } }
这篇关于在鼠标移动时获得bmp的rgb值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!