繁体   English   中英

从.DLL获取位图,转换为byte []和图像

[英]Get bitmap from .DLL, convert to byte[] and to image

我已收到用于测试目的的.DLL,此.dll包含稍后将用于处理来自硬件的实时图像的功能。

对于这个简单的.dll,我可以打开一个图像(加载到内存中),获取宽度和高度,并获得需要转换为图像的像素。 加载,获取宽度和获取高度很好,但获取像素并将其转换为位图或图像是一个问题。

我收到的C ++示例源代码:

   ApiFunc->OpenImageFile(this->OpenPictureDialog1->FileName.c_str());
   ApiFunc->AllocMemory();

   w = ApiFunc->GetImageWidth();
   h = ApiFunc->GetImageHeight();
   Image1->Width = w;
   Image1->Height = h;

   unsigned char* ptr = ApiFunc->GetImagePixels();
   COLORREF pixel;
   int r,g,b;
   for(int y=0; y<w; y++)
   {
       for(int x=0; x<h; x++)
       {
           r = (int)*(ptr+3*x);
           g = (int)*(ptr+3*x+1);
           b = (int)*(ptr+3*x+2);

           Image1->Canvas->Pixels[y][x] = RGB(r,g,b);
       }
       ptr += 3*h;
   }

在ApiFunc中,可以找到:

void __fastcall TAPIFunc::LoadDll(HINSTANCE m_hMain)
{
   //some others above
   GET_IMAGE_PIXELS  = (func_GET_IMAGE_PIXELS   )GetProcAddress( m_hMain, "GET_IMAGE_PIXELS");
   //some others below
}
unsigned char* __fastcall TAPIFunc::GetImagePixels(void)
{
   return GET_IMAGE_PIXELS();
}

所以现在我到目前为止尝试过,我已经尝试使用byte []作为返回参数,但抛出了MarshalDirectiveException。

    [DllImport("ImageTest.dll")]
    public static extern IntPtr GET_IMAGE_PIXELS();

    private void OpenImage(string filename)
    {
        OPEN_IMAGE_FILE(filename);
        ALLOC_MEMORY();
        int width = GET_IMAGE_WIDTH(); //=800
        int height = GET_IMAGE_HEIGHT(); //=600
        IntPtr buffer = GET_IMAGE_PIXELS();
        int size = width * height * 3;//not sure what the size must be, I think this is one of the issues, just following logic of one answer below.

        //but source: https://stackoverflow.com/a/16300450/2901207
        byte[] bitmapImageArray = new byte[size];
        Marshal.Copy(buffer, bitmapImageArray, 0, size);

        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
        IntPtr pNative = bmData.Scan0;
        Marshal.Copy(imageData, 0, pNative,size);
        bitmap.UnlockBits(bmData);
        bitmap.Save(Environment.CurrentDirectory + @"\result.bmp");
            using (var ms = new MemoryStream(bitmapImageArray))
            {
                //both throw exception: Parameter is not valid
                Bitmap bmp = new Bitmap(ms);
                Image bitmapImage = Image.FromStream(ms);
            }
    }

答案来源:

回答1,使用bitmap.LockBits方法 回答2和3,使用memoryStream

为了确保我有一个有效的图像进行测试,我在photoshop中保存了一个图像,使用此选项: photoshoppie

丑陋的测试图像:

旋转示例图像

结果: 很棒的结果

美不是吗? :)

还尝试使用for循环,并运行直到它崩溃。 运行直到count = 1441777,另一个图像计数= 1527793(相同尺寸)。

        int count = 0;
        for (int i = 0; i < width * height * 4; i++)
        {
            count++;
            bitmapImageArray[i] = Marshal.ReadByte(buffer, i);
        }

从错误的结果看来,水平/垂直实际上是切换的。 您的图像是通过逐列组织的像素获得的,而不是按行排列(通常如此)。

这是由您收到的示例源确认的:外部循环转到w(宽度),内部循环转到h(高度),尽管外部变量是y而内部变量是x,这是令人困惑的。

看来R和B组件也被切换了(我在这里没有解释,但请相信我)。

因此,在获得阵列后使用

byte [] bitmapImageArray = new byte [size]; Marshal.Copy(buffer,bitmapImageArray,0,size);

你必须重新组织它。 分配相同大小的另一个缓冲区bitmapImageArray2 ,循环遍历所有像素(逐行或逐列,如您所愿,但正确命名的变量与示例不同:x转到w,y转到h),并且将它写入目标数组:

bitmapImageArray2[(y * w + x) * 3] = bitmapImageArray[(x * h + y) * 3 + 2];
bitmapImageArray2[(y * w + x) * 3 + 1] = bitmapImageArray[(x * h + y) * 3 + 1];
bitmapImageArray2[(y * w + x) * 3 + 2] = bitmapImageArray[(x * h + y) * 3];

注意:您的size值似乎是正确的。

好吧,虽然这确实解决了问题,但我仍然不满意,当然我很高兴得到一些结果,但在我看来需要很长时间。 从实例化BitMap到保存之前需要:241ms。 虽然这可能看起来很小,但检索这样需要生成流畅视频的图像有点不对劲。

        for (int y = 0; y < width; y++)
        {
            for (int x = 0; x < height; x++)
            {
                int red = imageData[offset + 3 * x] ;
                int green = imageData[offset + 3 * x + 1];
                int blue = imageData[offset + 3 * x + 2];
                Color color = Color.FromArgb(red, green, blue);
                bitmap.SetPixel(y, x, color);
            }
            offset += 3 * height;
        }

@ Dim的回答有助于,首先进行转换,然后像以前一样处理,大幅提高速度。

结果如下:

    private void OpenImage(string filename)
    {
        OPEN_IMAGE_FILE(filename);
        ALLOC_MEMORY();
        int width = GET_IMAGE_WIDTH();
        int height = GET_IMAGE_HEIGHT();
        IntPtr buffer = GET_IMAGE_PIXELS();
        int size = width * height * 3;
        byte[] bitmapImageArray = new byte[size];
        Marshal.Copy(buffer, bitmapImageArray, 0, size);
        Bitmap bitmap3 = ConvertImage3(bitmapImageArray, height, width);
    }

    public Bitmap ConvertImage3(byte[] imageData, int height, int width)
    {
        int size = width * height * 3;
        byte[] bitmapImageArray2 = new byte[size];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                bitmapImageArray2[(y * width + x) * 3] = imageData[(x * height + y) * 3 + 2];
                bitmapImageArray2[(y * width + x) * 3 + 1] = imageData[(x * height + y) * 3 + 1];
                bitmapImageArray2[(y * width + x) * 3 + 2] = imageData[(x * height + y) * 3];
            }
        }

        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
        IntPtr pNative = bmData.Scan0;
        Marshal.Copy(bitmapImageArray2, 0, pNative, width * height * 3);
        bitmap.UnlockBits(bmData);
        return bitmap;
    }

现在我必须以某种方式提高从.DLL获取图像的速度,但这当然超出了这个问题的范围。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM