0

How do you get a bitmap from a graphics object (or at least a pointer to it's Scan0)?

If a graphics object really always refers to a bitmap, then it IS possible to get to the bitmap data from the graphics object. (Think: the graphics object HAS TO have a pointer to the bmp data. I'd code it in C but I'm on a project that requires everyone be hobbled by .NET.)

Applications of this would include things like: - using unsafe code to obtain faster screenshots - modifying what's on a control using CreateGraphics - (and the task I'm actually trying to accomplish which would take too long to explain)

Yes, this has been asked before but never answered. I'm not looking for how to get a graphics object from a bitmap (obviously trivial). FAIL1, FAIL2, FAIL3, FAIL4, FAIL5, FAIL6, FAIL7

Community
  • 1
  • 1
AppFzx
  • 1,445
  • 2
  • 14
  • 23
  • 2
    You mean something like this? http://stackoverflow.com/questions/1535730/convert-graphics-object-to-bitmap-object EDIT: Or rather, this constructor: http://msdn.microsoft.com/en-us/library/byca5y1f.aspx – Chris Sinclair Feb 04 '13 at 21:27
  • @Brian See the "FAIL" links at the bottom of my question for a good 20 or so approaches that don't work. I tried a few of those myself before searching and seeing they'd already been tried. – AppFzx Feb 04 '13 at 21:31
  • @AppFzx They are actually, they contain examples of getting the bitmap from a graphics object. – antonijn Feb 04 '13 at 21:31
  • "and the task I'm actually trying to accomplish which would take too long to explain" -- So, you don't want an answer to your question? – samuelesque Feb 04 '13 at 21:33
  • @Antonijn No.. The first link tells how to get a graphics from a bitmap. The second shows how to create a bitmap with the same pixelformat and size as a graphics object. – AppFzx Feb 04 '13 at 21:34
  • 3
    @AppFzx I'm not sure I understand _what_ exactly you _want_. Do you want to get a `System.Drawing.Bitmap` object that represents a snapshot of the current state of a `System.Drawing.Graphics` object? I gave you that. Do you want something that gives you access to the internal bitmap data that the `Graphics` object is _altering_ or _working with_? From what I understand, `Graphics` is just a wrapper around GDI+, which I'm not sure if there necessarily _is_ an underlying bitmap (I think it essentially directly interacts with system drivers and whatnot, could be wrong though) – Chris Sinclair Feb 04 '13 at 21:37
  • @dotsamuelswan I want a more broad answer that will help the other 7 people who have asked this question over the last 3 years and gotten no useful input. I'm trying to use multiple buffers and improve on the "pretend" transparency implemented by winforms for controls. I'd like to have the transparency of my control class show the background of the control underneath in most cases but in some cases show other child controls. Not very helpful to other StackOverflow users when it's that specific an application. – AppFzx Feb 04 '13 at 21:37
  • @ChrisSinclair What I need is the Scan0 pointer and Stride of the bitmap the graphics object is attached to. Since that isn't as useful to as many .NET people as the bitmap and since the bitmap *should* be more straightforward to get and since it's easy to go back and forth between (scan0 + stride + pixelformat) and Bitmap using bitmap's constructor or lockbits... I asked for graphics to bitmap. – AppFzx Feb 04 '13 at 21:41
  • 2
    Ahh sorry, slightly misunderstood. Do you have the original control source for the graphics object? I think you would need to force that control to repaint itself and obtain the bitmap that way. Since the `Graphics` is created from the various `From____` methods, I think you would need access to the original source that the Graphics object is drawing _to_. From what I understand, it just builds up a set of drawing/fill instructions and when flushed applies them. EDIT: Of course, now since you mention you're optimizing control transparencies, forcing a paint might be counterproductive. – Chris Sinclair Feb 04 '13 at 21:51
  • @ChrisSinclair I won't always have the original source. All I'm guaranteed is the graphics object. The original source is what I would like to access. – AppFzx Feb 05 '13 at 15:14

1 Answers1

2

I don't think what you're trying to do is possible since your assumption that "a graphics object really always refers to a bitmap" is false.

There's a good article here that shows how to render a control to bitmap if you really want a bitmap and another one here that shows how to quickly update the screen at the WndProc level. If you're more familiar with C++ that might get you going the right direction.

Still.Tony
  • 1,437
  • 12
  • 35
  • 1
    If what you're saying is correct and ["The Graphics class is a surface to an image and is already a bitmap."](http://stackoverflow.com/a/1535748/1048432) is not true, then this does solve my particular situation... Thanks – AppFzx Feb 05 '13 at 16:18