C#的WinForms:获得具有屏幕显示图像是一个控制背后 [英] c# winforms: Getting the screenshot image that has to be behind a control
问题描述
我的C#Windows形式,它有它几个控件,这些控件的部分都位于一个在另一个。我想,将采取输入从形式的控制和将返回具有成为控制背后的图像的功能。为前:如果表单有和backgroundImage,并包含一个按钮 - 如果我要跑这个功能我就明白了位于后面的按钮和backgroundImage的一部分。任何想法 - 和code
I have c# windows form which have several controls on it, part of the controls are located one on another. I want a function that will take for input a control from the form and will return the image that has to be behind the control. for ex: if the form has backgroundimage and contains a button on it - if I'll run this function I'll got the part of backgroundimage that located behind the button. any Idea - and code?
H-E-L-P!
推荐答案
这是我最初的猜测,但必须对其进行测试。
That's my initial guess, but have to test it.
- 把按钮无形
- 捕捉当前画面
- 捕获按钮的clientRectangle作物屏幕
-
Restablish按钮。
- Put button invisible
- capture current screen
- Crop screen captured to the clientRectangle of the button
Restablish button.
public static Image GetBackImage(Control c) {
c.Visible = false;
var bmp = GetScreen();
var img = CropImage(bmp, c.ClientRectangle);
c.Visible = true;
}
public static Bitmap GetScreen() {
int width = SystemInformation.PrimaryMonitorSize.Width;
int height = SystemInformation.PrimaryMonitorSize.Height;
Rectangle screenRegion = Screen.AllScreens[0].Bounds;
var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);
return bitmap;
}
公共静态无效CropImage(图片imagenOriginal,矩形areaCortar){
图形G = NULL;
尝试{
//创建目标(裁剪)位图
VAR bmpCropped =新位图(areaCortar.Width,areaCortar.Height);
//创建图形对象进行绘制
G = Graphics.FromImage(bmpCropped);
} public static void CropImage(Image imagenOriginal, Rectangle areaCortar) { Graphics g = null; try { //create the destination (cropped) bitmap var bmpCropped = new Bitmap(areaCortar.Width, areaCortar.Height); //create the graphics object to draw with g = Graphics.FromImage(bmpCropped);
var rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height);
//draw the areaCortar of the original image to the rectDestination of bmpCropped
g.DrawImage(imagenOriginal, rectDestination, areaCortar, GraphicsUnit.Pixel);
//release system resources
} finally {
if (g != null) {
g.Dispose();
}
}
}
这篇关于C#的WinForms:获得具有屏幕显示图像是一个控制背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!