如何使用.net中的不安全编码获取位图图像所选区域的RGB值 [英] how to take the RGB value of the selected region of the bitmap image using unsafe coding in .net

查看:143
本文介绍了如何使用.net中的不安全编码获取位图图像所选区域的RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用.net中的不安全编码获取位图图像所选区域的RGB值
我需要更改所选的特定区域,并提供所选图像的起始位置的i和j值.


整个代码是

How do I take the RGB value of the selected region of the bitmap image using unsafe coding in .net
I need to change the particular region that I have selected and giving the i and j value of the starting position of the selected image.


The entire code is

//   selection is the rectangle from the mouse selected region from the picturebox....
bitmap bmp2 = new bitmap(picturebox1.image);
bitmapdata data = bmp2.lockbits(selection,ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
     byte *p = (byte*)(data.scan0);
     offset = data.stride - bmp.width*3;
     for(i=selection.x; i< limitx;i++)
     {
          for(j=selection.y;j<limity;j++)
          {
               position = j*bmp2.width *(3) +i*3+j*offset;//what is line used for i dot know....
               b = p [ position +j];     ///need to take the RGB value
               g = p [position+j+1];
               r = p [ position + j +2];
               p+=3;
          }
          p+=offset;
     }
}
picturebox.image = bmp2.image;   // that should display the selected region in any color....
bmp2.unlockbits(data);
bmp2.dispose();



它无法正常工作.

请帮助我...



It''s not working properly.

Please help me...

推荐答案

如果原始位图像素格式不是PixelFormat.Format24bppRgb,则此代码会弄乱事情.在所有情况下都应检查格式.

尽管您打算反转位的子集,但是此代码实际上反转了所有位.如果子集是矩形,则将其反转是微不足道的.在这种情况下,必需的矩形应作为LockBits的第一个参数传递.
位图数据的实际更新由UnlockBits完成.这就是为什么要将较小尺寸的矩形作为参数LockBits总是更好的原因,因为您想获得更好的性能.

此建议也适用于您的子集不是矩形的情况.在这种情况下,您可以计算要修改的位周围的边界矩形.如果此边界矩形的截距小于整个位图矩形的截距,则可以再次获得更好的性能.
This code will mess up things in case original bitmap pixel format is not PixelFormat.Format24bppRgb. The format should be checked up in all cases.

Despite of your intent to invert a subset of bits, this code actually invert all bits. Inverting a subset is trivial if it is a rectangle. In this case, the required rectangle should be passed as a first parameter of LockBits.

Actual update of the bitmap data is done by UnlockBits. That''s why passing a rectangle of smaller size as a parameter of LockBits is always better is you want to have better performance.

This advice also valid of your subset is not rectangular. In this case, you can calculate bounding rectangle around the bits you''re going to modify. If the interception of this bounding rectangle is less then the whole bitmap rectangle, you can have better performance again.


我所看到的代码对图像没有任何作用.看起来,如果将循环中获得的rgb值保存到某个结构的数组中,则将获得所有像素(即全部)的rgb值.上面的代码本身不执行任何操作.
如果要获取一个矩形截面作为单独的图像,则可以锁定该矩形的位并将该数据的scan0读取到位图中.
The code as I see does nothing to the image. Looks like if you save the rgb values that you get in the loop into an array of some structure you get the rgb values for all the pixels, thats all. The above code in itself does not do anything.
If you want to get a rectangular section as a seprate image, the you can lock bits of that rectangle and read the scan0 of that data into a bitmap.


try
      {
        Bitmap bmp = new Bitmap(250, 50);
        //   selection is the rectangle from the mouse selected region from the picturebox....
        Bitmap bmp2 = new Bitmap(@"C:\Users\vinayakk\Desktop\tenant_notselected.jpg");
        BitmapData data = bmp2.LockBits(new Rectangle(0, 0, 250, 50), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        BitmapData dta1 = bmp.LockBits(new Rectangle(0, 0, 250, 50), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
      
      unsafe
      {
        //byte* p = (byte*)(data.Scan0);
        //var offset = data.Stride - bmp2.Width * 3;
        //for (int i = 10; i < 50; i++)
        //{
        //  for (int j = 10; j < 50; j++)
        //  {
        //    var position = j * bmp2.Width * (3) + i * 3 + j * offset;
        //    //what is line used for i dot know....               
        //    byte b = p[position + j] = (byte)255;
        //    // need to take the RGB value              
        //    byte g = p[position + j + 1] = (byte)255; byte r = p[position + j + 2] = (byte)255; p += 3;
        //  } p += offset;
        //}
      }
      // that should display the selected region in any color....
        dta1.Scan0 = data.Scan0;
        bmp2.UnlockBits(data);
        bmp.UnlockBits(dta1);
        
        bmp.Save("--Your path.jpg--");
      bmp2.Dispose();
      }
      catch (Exception)
      {
        throw;
      }



更改路径.您的选择已保存.取消注释循环中的代码,即可更改像素颜色.



Change the paths. Your selection is saved. Uncomment the code in the loop the pixel colors are changed.


这篇关于如何使用.net中的不安全编码获取位图图像所选区域的RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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