wpf - Showing part of UserControl like CroppedBitmap -
i can't english yet. please understand if can't understand me clearly.
i have huge data table in usercontrol.xaml, downscale usercontrol object showing whole in mainwindow.
i want same size datatable showing of partially usercontrol in mainwindow.
like image display way:
<image> <image.source> <croppedbitmap source="<path source image>" sourcerect="20,20,50,50"/> </image.source> </image>
showing usercontrol
in mainwindow sourcerect.
if understand correctly, have several options. first way, , think easiest use viewbox
control.
1. viewbox
the viewbox
control inherited decorator
used stretch or scale child element, scaled proportionally, ie can not set him size such 300x100.
example
<viewbox width="300" height="300"> <datagrid> ... </datagrid> </viewbox>
the second way use screen capture of control, show, , if want use croppedbitmap
.
2. capturing screen
i found great article pete brown
on subject here:
capturing screen images in wpf using gdi, win32 , little wpf interop help
in article example, , looks this:
screencapture
class screencapture { public static bitmapsource capturefullscreen(bool addtoclipboard) { return captureregion( user32.getdesktopwindow(), (int)systemparameters.virtualscreenleft, (int)systemparameters.virtualscreentop, (int)systemparameters.virtualscreenwidth, (int)systemparameters.virtualscreenheight, addtoclipboard); } // capture window. doesn't alt-prtscrn version loses window shadow. // version captures shadow , optionally inserts blank (usually white) area behind // keep screen shot clean public static bitmapsource capturewindow(intptr hwnd, bool recolorbackground, color substitutebackgroundcolor, bool addtoclipboard) { int32rect rect = getwindowactualrect(hwnd); window blankingwindow = null; if (recolorbackground) { blankingwindow = new window(); blankingwindow.windowstyle = windowstyle.none; blankingwindow.title = string.empty; blankingwindow.showintaskbar = false; blankingwindow.allowstransparency = true; blankingwindow.background = new solidcolorbrush(substitutebackgroundcolor); blankingwindow.show(); int fudge = 20; blankingwindow.left = rect.x - fudge / 2; blankingwindow.top = rect.y - fudge / 2; blankingwindow.width = rect.width + fudge; blankingwindow.height = rect.height + fudge; } // bring to-be-captured window capture foreground // there's race condition here blanking window // comes top. hate those. there surely // non-wpf native solution blanking window // involves drawing directly on desktop or target window user32.setforegroundwindow(hwnd); bitmapsource captured = captureregion( hwnd, rect.x, rect.y, rect.width, rect.height, true); if (blankingwindow != null) blankingwindow.close(); return captured; } // capture region of full screen public static bitmapsource captureregion(int x, int y, int width, int height, bool addtoclipboard) { return captureregion(user32.getdesktopwindow(), x, y, width, height, addtoclipboard); } // capture region of screen, defined hwnd public static bitmapsource captureregion( intptr hwnd, int x, int y, int width, int height, bool addtoclipboard) { intptr sourcedc = intptr.zero; intptr targetdc = intptr.zero; intptr compatiblebitmaphandle = intptr.zero; bitmapsource bitmap = null; try { // gets main desktop , open windows sourcedc = user32.getdc(user32.getdesktopwindow()); //sourcedc = user32.getdc(hwnd); targetdc = gdi32.createcompatibledc(sourcedc); // create bitmap compatible our target dc compatiblebitmaphandle = gdi32.createcompatiblebitmap(sourcedc, width, height); // gets bitmap target device context gdi32.selectobject(targetdc, compatiblebitmaphandle); // copy source destination gdi32.bitblt(targetdc, 0, 0, width, height, sourcedc, x, y, gdi32.srccopy); // here's wpf glue make work. converts // hbitmap bitmapsource. love wpf interop functions bitmap = system.windows.interop.imaging.createbitmapsourcefromhbitmap( compatiblebitmaphandle, intptr.zero, int32rect.empty, bitmapsizeoptions.fromemptyoptions()); if (addtoclipboard) { //clipboard.setimage(bitmap); // high memory usage large images idataobject data = new dataobject(); data.setdata(dataformats.dib, bitmap, false); clipboard.setdataobject(data, false); } } catch (exception ex) { throw new screencaptureexception(string.format("error capturing region {0},{1},{2},{3}", x, y, width, height), ex); } { gdi32.deleteobject(compatiblebitmaphandle); user32.releasedc(intptr.zero, sourcedc); user32.releasedc(intptr.zero, targetdc); } return bitmap; } // accounts border , shadow. serious fudgery here. private static int32rect getwindowactualrect(intptr hwnd) { win32rect windowrect = new win32rect(); win32rect clientrect = new win32rect(); user32.getwindowrect(hwnd, out windowrect); user32.getclientrect(hwnd, out clientrect); int sideborder = (windowrect.width - clientrect.width)/2 + 1; // sooo, yeah. const int hacktoaccountforshadow = 4; win32point topleftpoint = new win32point(windowrect.left - sideborder, windowrect.top - sideborder); //user32.clienttoscreen(hwnd, ref topleftpoint); int32rect actualrect = new int32rect( topleftpoint.x, topleftpoint.y, windowrect.width + sideborder * 2 + hacktoaccountforshadow, windowrect.height + sideborder * 2 + hacktoaccountforshadow); return actualrect; } }
example of using:
private void captureregionbutton_click(object sender, routedeventargs e) { capturedimage.source = screencapture.captureregion(100, 100, 500, 500, true); } private void capturescreenbutton_click(object sender, routedeventargs e) { capturedimage.source = screencapture.capturefullscreen(true); }
Comments
Post a Comment