C# 如何在此函数中调整位图的大小?

C# 如何在此函数中调整位图的大小?,c#,C#,所有时间的大小都是1920x1080,但我希望它是例如10x10,这样函数中的过程会更快。我不介意位图的大小或它的质量,我只是希望它更快 在这个函数中,我创建每个位图的直方图。问题是FOR循环需要很长时间 public static long[] GetHistogram(Bitmap b) { long[] myHistogram = new long[256]; // histogram סופר כמה יש מכל גוון

所有时间的大小都是1920x1080,但我希望它是例如10x10,这样函数中的过程会更快。我不介意位图的大小或它的质量,我只是希望它更快

在这个函数中,我创建每个位图的直方图。问题是FOR循环需要很长时间

public static long[] GetHistogram(Bitmap b)
        {
            long[] myHistogram = new long[256]; // histogram סופר כמה יש מכל גוון

            BitmapData bmData = null;

            //b = new Bitmap(100, 100);
            //b.SetPixel(0,0,Color.FromArgb(10,30,40,50)); //run and make it come to here


            try
            {
                ResizeBitmap(b, 10, 10);
                //Lock it fixed with 32bpp
                bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                int scanline = bmData.Stride;

                System.IntPtr Scan0 = bmData.Scan0;
                unsafe
                {
                    byte* p = (byte*)(void*)Scan0;
                    int nWidth = b.Width;
                    int nHeight = b.Height;

                    for (int y = 0; y < nHeight; y++)
                    {
                        for (int x = 0; x < nWidth; x++)
                        {
                            long Temp = 0;
                            Temp += p[0]; //  p[0] - blue, p[1] - green , p[2]-red
                            Temp += p[1];
                            Temp += p[2];

                            Temp = (int)Temp / 3;
                            myHistogram[Temp]++;

                            //we do not need to use any offset, we always can increment by pixelsize when
                            //locking in 32bppArgb - mode
                            p += 4;
                        }
                    }
                }

                b.UnlockBits(bmData);
            }
            catch
            {
                try
                {
                    b.UnlockBits(bmData);
                }
                catch
                {

                }
            }

            return myHistogram; // to save to a file the histogram of all the bitmaps/frames each line contain 0 to 256 values of a frame.
        }

也许还有另一种方法可以使GetHistogram函数更快地工作?

该方法返回新位图,因此您应该使用返回值:

Bitmap newBitmap = ResizeBitmap(b, 10, 10);
b.Dispose();
b = newBitmap;

旁注:您忽略了图像的步幅,这意味着指针可以超出图像数据。如果位图存储在上下,步幅是负的。

Guffa你能告诉我如何用步幅来做吗?告诉我我的代码应该是什么样子?请谢谢。Ok更改了我在顶部函数GetHistogram中所做/添加的大小:public static long[]GetHistogram(位图b){long[]myHistogram=new long[256];BitmapData bmData=null;Bitmap newBitmap=ResizeBitmap(b,10,10);b.Dispose();b=newBitmap;现在该做什么以及如何使用步幅?@user1741587:每行之后:
p+=scanline-nWidth*4;
Bitmap newBitmap = ResizeBitmap(b, 10, 10);
b.Dispose();
b = newBitmap;