Image.FromHbitmap exception: A generic error occurred in GDI+

manjunath 1 Reputation point
2020-12-12T00:30:58.943+00:00

47561-capture.png

I am trying to create bitmap from dib handle in order to show it in picturebox . But i keep getting this error and not sure how to fix this problem. I did search for error in online and most resources points out that the inptr is zero so error pops up. but thats not my case.

Can anyone explain me how to fix this.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,574 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Cheong00 3,471 Reputation points
    2020-12-12T01:26:14.863+00:00

    I think that you can't draw Device Independent Bitmap directly with GDI+ as it doesn't use the bitmap header of the bitmap. You need to create your own DIBSection and apply to it first (Ignore (3) because you're not drawing towards the DIBSection with GDI+).

    You can refer to how to do it here (It's in C++ code but not too difficult to relate it to C#, as except the GetDC() call will be Graphics.GetHdc() and ReleaseDC() will be Graphics.ReleaseHdc(), the other things will be calling P/Invoke to corresponding GDI APIs.

    Remember to call DeleteObject() to free the DIBSection afterwards.

    0 comments No comments