Output control in Bitmap - winforms

I want to implement previews/thumbnails in my project, therefor i need the graphical output of a control as bitmap. I have a third party control which loads documents and displays them. Is it possible to fetch the output of an control and store it in a bitmap object without adding it to the UI? And If how?
Edit: I probably should said that before, but i don't know if that's important. The ThirdParty Control is an OCX(ActiveX control).

in a Form do:
Bitmap myBitmap = new Bitmap(button1.Width, button1.Height);
// button1.Draw..., not 'this.Draw...'!
button1.DrawToBitmap(myBitmap, button1.DisplayRectangle);
myBitmap.Save(#"C:\test.bmp");

Related

WPF MVVM binding controls with helper methods PDFTron PDFViewer

I am struggling with a WPF MVVM problem using a control that has helper functions and exposing those helper functions to my viewmodels.
I am using the PDFTron viewer control that shows a PDFDocument object. Items in that PDFDocument are specified in terms of a PDFTron.Rect structure with the elements measured in points(1/72th inch) and a page number
To help convert the coordinate systems to and from screen position the PDFTron ViewerControl has various function on itself.
e.g. To convert from a screen point
Double x, y;
int page = Control.GetPageNumberFromScreenPt(x, y);
Control.ConvScreenPtToPagePt(ref x, ref y, page);
To convert to a screen point is from the object being examined
PDFTron.Rect r = Control.GetScreenRectForAnnot(embeddedObject, pageNumber);
What the best way of calling/exposing or binding to functions like this between the view model(s) and the PDF control as I would really like to databind to coordinates.
For instance I have an adorner defined in XAML that allows me to move an image I have read from the embeddedobject about the page by dragging and I can also resize the image. The viewmodel knows As my view model knows the image the embedded object its page and Rectangle on that page, but as PDF coordinates. But this needs to be translated to screen coordinates for the X, Y, Width and Height for binding to the XAML Attributes. I cannot quite see how to do this as it seems beyond a dataconverter.
So the control in the xaml has attributes measured in screen units
AdornerLeft="{Binding Data.X, Mode=TwoWay}"
AdornerTop="{Binding Data.Y, Mode=TwoWay}"
AdornerWidth="{Binding Data.Width, Mode=TwoWay}"
AdornerHeight="{Binding Data.Height, Mode=TwoWay}"
If the X and Y change I need the change to be reflected eventually as the page,x,y coordinates of the viewer as they are what are used by the underlying model.
I did wonder would it be wise to make a series of dependency properties so if I change one it ripples the change through the others as a conversion? So for example I have a property ScreenY When that changes it updates PDFY and PDFPage and vice versa but that seems overly complicated. Any suggestions?
Typically what is done, is that during user interaction, so while your user is moving and resizing the image, everything is drawn overtop of PDFViewWPF viewer.
You can get a Canvas object from PDFViewWPF.GetCanvas() and then you can draw your image on that if you like.
At this point, nothing relates to the PDF, you are just dealing with WPF coordinates.
Only once the user is done moving and dragging, and you want to add the image to the PDF page, perhaps as a Stamp annotation, or even injecting the image into the page content, only then would you erase all your graphics, and inject the image into the PDF. Only at this point would you need to translate between coordinate systems.
Please take a closer look at the PDFViewWPFTools project, and see how something like the Rectangle annotations are created.
This post might provide additional clarity for you.
I ended up using the Prism library and the eventAggregator to pass the details back to a the View and get the results, also using events to refresh the view. The PDFWPFViewer MVVM sample from PdfTron beside having some issues with incorrect bindings, essentially uses a tool library that is shared and acts like a windows forms library, no behaviors for controlling adorner drag etc.

WriteableBitmap used in Mat.Mat constructor (Emgu.CV)

what I'm trying to do is using an let say pre-loaded image within Emgu.CV.Mat. It offers an constructor where its possible to specify a filename. But my image is already loaded (and manipulated) within the code, therefore it makes no sense to save the file here just to load it again.
I'm working within a WPF Environment and therefor have my image currently as a WriteableBitmap. Furthermore I found couple of hints (e.g. here) how to convert an Mat to WriteableBitmap. But what I'm looking for is the other way round, using a WriteableBitmap in order to Emgu.CV stuff with it.
According to Emgu CV 2.x it was possible to use a Bitmap but it seams that this feature was removed in Emgu CV 3.x.
Any ideas how to do it in Emgu CV Version 3?
I have been experimenting in this area for a work related application. To effectively use a WriteableBitmap with a Mat or an Image<,> you need access to the raw pixels.
Because I have images coming from hardware as well as from creating a RenderTargetBitmap what I found works for me is to allocate a buffer using HGlobalAlloc that is the proper size and use RenderTargetBitmap.CopyPixels() to move the data to this buffer. I also create an Image using an IntPtr to this data. Once I am done with my manipulations I use a WiteableBitmap.WritePixels() to put the image back into the WriteableBitmap.
Here is also a solution from Emgu: BitmapSourceConverter
Doug

Can I create a motion colorizing pixel shader in WPF?

I have a video playing of lines being drawn on the screen. Is it possible to create a pixel shader (for WPF) that turns newly colored pixels a certain color for N milliseconds?
That way, there can be some indication to the user to movement on the screen when the lines don't move often and the user isn't always looking at the screen.
You can use DirectShow. Its written in unmanaged code, so you need to use this wrapper DirectShow.NET in order to use it in your C# application which is running in managed environment (samples are included, even with EVR which stands for Enhanced video Renderer which means MUCH better video quality). And when you will be passing a control handle to wrapper method for setting the video output, you need a WinForms control, because only from them you can get your desired control handle. That WinForms control you can then host in your WPF application using the WindowsFormsHost control provided for such situations when you need to use some WinForms control(s) in a WPF application. Its just theory, so i dont know if its an ultimate solution for you.
BTW: The whole idea is based on fact, that DirectShow is just some query constructed from separated filters. Renderer is a filter (EVR, VMR-7, VMR-9). Sound player is a filter. And they are connected through their pins. Its like a diagram. Electronic schema or something like that. And you can put for example Grey scale filter in there. And voila, video output will be greyscale. There is a bunch of tutorials for that. And completed simple filters as well. Unfortunately, filters must be written in C++:(
PS: I never said its gonna be easy:D

How do I create a WPF Image control that sources its data from a JPEG I've read into memory?

For the images in my application, I have been setting the source property of my image to a JPEG file on disk but, as part of my next iteration, I want to test keeping them in memory for speed.
How do I tell the WPF Image control to get its information from an in-memory source rather than from a file?
Have a look at InteropBitmap and WriteableBitmap, two classes that inherit from BitmapSource that allow you to supply the pixels of the image from an array.
To create an InteropBitmap you use methods on the Imaging class like Imaging.CreateBitmapSourceFromMemorySection.

Make part of an image transparent

I want to put an image on a button, but I want part of the image to be transparent. How do I do this?
Try the Image.OpacityMask property. You can give it a brush that specifies the region you want to be transparent.
EDIT: From MSDN:
There is no direct support for
color-keying a bitmap in WPF.
However, it is fairly easy to
implement on your own. Dwayne has
implemented a ColorKeyBitmap on his
blog:
http://blogs.msdn.com/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx
I believe it links to the code on
Codeplex as well. You could also
accomplish this simply by reading your
bitmap into system memory, iterating
through all the pixels and setting
their values yourself, and
constructing a new bitmap out of that
array.
Use a paint program (I use Paint.Net) to change the area you want transparent to an alha=0 color. Then save the image (mine was JPG) as a PNG. Seemed to work fine for me in the WPF Image control.

Resources