
Clipboard to Texture?
Posted Thursday, 14 June, 2012 - 09:33 by Iluvatar inHi there!
I have a big problem, because I need to get my rendered WPF-window into a texture somehow.
I have already mastered the part where I render a control (my WPF-window) to a RenderTargetBitmap!
The I use this RenderTargetBitmap and Copy it to the Clipboard.
How is it possible to go on from that point?
Or, if this does not work the easy way, is it possible to create a "Bitmap" or an "Image" out of the Clipboard Data?
Thank you very much :)
Iluvatar


Comments
Re: Clipboard to Texture?
Ok I found a solution to the problem, but I almost instantly got another one :) (as usual)
I need to do the following,
I have to implement this:
Context Preserving Visual Links - PDF
I use a WPF UI to draw all elements of the user-interface.
I already (almost) created the functionality to display different views of one of the hard-drives in the computer, I only need to implement the OpenGL-part of the paper linked above.
So I created a WinFormsHost object and inserted my "CustomGLControl" as described in the GraphicsContext - Class description. It "lies" above the other user-interface - elements (zIndex = 2 for the WinFormsHost)
How is it possible to add the (Custom)GLControl without "visually" interfering with my program-user-interface?
I tried this (and in the designer it is not shown at all, but the whole program is "grey", if I start it):
Or maybe is it the wrong approch all along?
If you don't want to read the paper, this is, what i try to do:
I have some parts of the screen that should be "highlighted" and connected through links (lines on the screen).
The algorithm is image-based, so I need to get a Screenshot of the monitor/program window (whatever) and do some pomputations based on that screenshot.
One thing is to calculate a "penalty-map" based on color differences for eample!
Using this penalty-map i need to compute the links between the highlighted positions on the screen.
So I though I would use OpenGL for those computations.... Or do you think there's an easier way to do just that??? (using a WPF-Canvas element for example???)
Thank's for the help!!
Iluvatar
Re: Clipboard to Texture?
Or maybe asked differently:
What do you think?
Initializing OpenGL without control, then doing the calculations (in fact render to a texture at the end) and then use the result as (partly) transparent texture for the canvas control??
Thank's for any advise! :)
Ilu
Re: Clipboard to Texture?
I've taken the liberty to correct the link to the PDF in your post above, it didn't show.
There is no need to show the OpenGL window in order to render to it, but there must be a context present (need framebuffer and function pointers etc.). Also PBO and FBO Extensions can be used for offscreen rendering.
Another method to get the picture from WPF to OpenGL may simply be write it on the harddisk or some globally accessible
public static Queue<Image> RawInput;I haven't said anything to this yet, because I don't really see why they use OpenGL (besides speed) you can also implement this on the CPU with .Net 4 parallelism, it will probably be not as fast as the GPU version but it will be easier to tweak/adjust/improve (and most of all debug) the technique until it becomes what you want. When you are done with all features you want to add, then you can still port it to OpenGL or CL.
Re: Clipboard to Texture?
Thank you for your reply (and editing the Link - but it worked for me, I tested it...)!
I did some things in the meantime:
I totally forgot about the last line!!!
I hope the last two points work as expected, because I didn't have the time to test yet.
So the Steps include (in the End :) ):
Do you think this approch is ok or would YOU prefer the .NET variant?
Iluvatar
Re: Clipboard to Texture?
If you want to change it in some way, like drawing proper splines or allow the user to load some kind of layout texture which is used to further protect regions of the image, using GL is already an optimization and will make changing/debug things alot harder. (may break some assumptions the optimization is based on)
If it is exactly what you want and you don't need to change a bit, implement it with OpenGL/CL (for the benefit of speed).
Re: Clipboard to Texture?
Another question related to this :)
When I grab a Screenshot, I get an "WriteableBitmap" (Syste.Windows.Media.WriteableBitmap). My Texture Creation only allows "old" Bitmaps (System.Drawing.Bitmap)....
I already searched the Web for a Conversion but I wasn't able to find a solution yet.
Maybe you could tell me, how I can "convert" the WriteableBitmap" to an older "Bitmap" or maybe you can come up with a hit, how to rewrite my Constructor??
Here is the "Texture" - Constructor using a Bitmap :) :
Thank you!
Re: Clipboard to Texture?
From http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.wri...
Ignore step 3&4, the IntPtr from BackBuffer can be fed to GL.TexImage2D. (examine Format property for other params)
Re: Clipboard to Texture?
Oh thank you!
I am going to try it when I am home again!
I'll keep you posted!
BTW:
Thank you for the support all the time!! :)!!
Re: Clipboard to Texture?
Ok, I tried it out now,
I am able to create a Texture now, but I think my Pixel-Format was not right (was it??)!
When I saved the WriteableBitmap and the Bitmap to Disc I found out, that the Texture was in fact... black!
What I did, was exactly what you mentioned:
Is it possible, that my Code for Reading the Pixels is not ok? I mean, I it is there, to just read Pixels from the actually bound FrameBuffer right (THEN I understand, why I only get black Images :) )?
Here is the Code for creating a Bitmap and for saving to a File (or is not working this way at all?)
BUT: I found a way to create a System.Drawing.Bitmap out of a WriteableBitmap! It's only a hack, but for now it works :)
Here is the Code (implemented as an Extension and using a MemoryStream):
When I try to create a Bitmap using my "GetGLBitmapData()" , it doesn't show anything (only a black Image), but I think my Texture is well, because I just call my (well working and tested ) Constructor....
And if I save the converted System.Drawing.Bitmap to the Disc, the Image looks ok :)
I will continue, to tell you, if the Texture is ok :)
Re: Clipboard to Texture?
So I tried many different possibilities to access the Texture.
I have no Problem saving the Screenshot to a File (through a Bitmap), but I also need to create a Bitmap out of another Texture...
The other Texture is the "ColorAttachment0" of a FramebufferObject!
Well, for now, I do the following:
Inizialize my FBO and Color-Attachment (WORKS, this is also tested!) and I grab the Screenshot and create another Texture!
I use a simple GLSL Shader to just pass the Color-Values for now onto a "ScreenAlignedQuad"
I make the Conext current
Setup the Viewport
Bind the Framebuffer
Use the right Shader
Bind the ScreenShotTexture (input for the Shader)
Render the ScreenAlignedQuad
Unbind the Objects again.
-> At this Point I want to Create a Bitmap out of the Color-Attachment0 of the FBO...
All I get is a black Texture all the time...
i used the "BitmapData" and a Bitmap.Lock() to achive this but It doesn't work...
The Code works as described above!
I also tried to do something like this instead:
return new Bitmap(m_data.Width, m_data.Height, m_data.Stride, m_data.PixelFormat, m_data.Scan0);and I also tried to get the Bitmap-Data like this:
But nothing works.... (tried all, when the FBO was still bound, or not)
I never get any Errors, not when I create/bind/use/... the Shader/FBO/Texture... all seems ok.
Anybody any suggestions?