提高C#中GetPixel函数的速度 [英] improving speed of GetPixel function in c#

查看:261
本文介绍了提高C#中GetPixel函数的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要快速读取bmp的getpixel,但是它非常低 我使用了LockBits

i need to read getpixel of bmp with speed but is very low i used of LockBits

     private void LockUnlockBitsExample(Bitmap bmp)
    {

        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData =
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);
        IntPtr ptr = bmpData.Scan0;

        int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
        rgbValues = new byte[bytes];

        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
        bmp.UnlockBits(bmpData);
    }

和此功能

        private Color GetMyPixel(byte[] rgbValues,Bitmap bmp, int x,int y )
    {

        int index= (bmp.Width*y+x)*3;
        Color MyColor = Color.FromArgb(rgbValues[index], rgbValues[index + 1], rgbValues[index + 2]);
        return MyColor;
    }

但是我函数的输出不同于原始的getpixel

but output of my function is different from original getpixel

推荐答案

出于某些原因,我在VB中有代码,几乎可以完成与您完全相同的操作,因此希望对您有所帮助.您可以尝试对GetMyPixel进行以下修改:

I have code in VB for some reason that does almost the exact same thing as you, so I hope this helps. You can try the following modification to GetMyPixel:

使用Stride而不是Width,并在调用FromArgb时反转字节顺序.

Use Stride instead of Width and invert the byte order in your call to FromArgb.

private Color GetMyPixel(byte[] rgbValues,Bitmap bmp, int x,int y )
{
   int index= (bmp.Stride*y+x*3);        
   if (index > rgbValues.Length - 3)
   index = rgbValues.Length - 3;
   Color MyColor = Color.FromArgb(rgbValues[index+2], rgbValues[index + 1], rgbValues[index]);         
    return MyColor;
} 

这篇关于提高C#中GetPixel函数的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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