我怎样才能捕捉为位图什么只一个PictureBox显示的是,不使用"从屏幕&QUOT复制;? [英] How can I capture as bitmap only what a picturebox is displaying, without using "copy from screen"?

查看:131
本文介绍了我怎样才能捕捉为位图什么只一个PictureBox显示的是,不使用"从屏幕&QUOT复制;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体做法是:我需要捕获作为位图一个什么样的图片框实际显示的特定区域。该地区的坐标是由我在PictureBox的顶部重叠(但属于在PictureBox)的控制的范围规定。控制是隐藏的,当我做快照区域。

Specifically: I need to capture as a bitmap a specific region of what a picturebox is actually displaying. The coordinates of the region are specified by the bounds of a control that I have overlayed on top of the picturebox (but that belongs to the picturebox). The control is hidden when I make the "snapshot" of the region.

我试图用普通屏幕捕获方法(CopyFromScreen),但你不能真正控制时序那里。所以这是捕捉质的状态,就像在我的PictureBox的照片之间的过渡。经常有人仅捕获纯黑色图像(图像框的背景颜色)。

I tried using normal screen capture methods (CopyFromScreen), but you can't really control the timing there. So it was capturing "interstitial" states, like transitions between photos in my picturebox. Frequently it was only capturing purely black images (the background color of the picture box).

于是,我只是转换图像(picturebox.image属性)显示为位图。问题还有,该图片框很少显示出完全的形象。它的显示图像,缩放和裁剪为适当的它的某些部分是sizemode(这是变焦)。所以我不能把我的控制坐标和图像作为整个剪辑他们。

So I tried just converting the image (picturebox.image property) being displayed to a bitmap. The problem there is that the picture box is rarely showing exactly the image. It's displaying some PORTION of the image, scaled and clipped as appropriate to it's sizemode (which is zoom). So the I can't just take my control coordinates and clip them from the image as a whole.

于是,我就估计正在显示的内容部分的图像,并根据该纠正我的矩形。原来,我基本上是重新打造的放大$ C $的图片框的C ++做在PictureBox这(使用长宽比,图像的宽高比,在缩放什么水平猜测正在发生的图像如果它比在PictureBox等),或大或小。这不是pretty。

So I tried to estimate what portion of the image was being displayed, and correcting my rectangle based on that. Turns out that I was basically re-creating the "zoom" code of the picturebox to do this (using aspect ratio of the picturebox, the aspect ratio of the image, guessing at what level of scaling is currently happening to the image if it's larger or smaller than the picturebox, etc). It was not pretty.

所以:现在我需要的只是仅捕捉当前显示的图片框的客户区位图,包括照片和任何黑宽银幕目前正在围绕它显示的方法。任何人有一个?

So: now I need a method of just capturing only the bitmap currently being displayed in the client area of the picturebox, including the photo and any black "letterboxing" currently being displayed around it. Anybody got one?

请记住,我不能依靠使用CopyFromScreen。这不是我的目的,足够可靠。我想我需要让图片框告诉我它显示位的方法。

Remember that I can't rely on using CopyFromScreen. It's not reliable enough for my purposes. I think I need a method of getting picturebox to TELL me the bits it is displaying.

推荐答案

这将复制并保存图片框的当前显示的内容包括的BackgroundImage (如果存在的话,如果它照过),并且所有的控制属于图片框,如标签等。此外,还包括在油漆事件绘制的元素。事情绘制的之外油漆事件都是非持久和意志的不可以被包括在内。

This will copy and save the currently shown content of the PictureBox including a BackgroundImage (if there is one and if it shines through) and also all Controls that belong to the PictureBox, like Labels etc.. Also included are elements drawn in the Paint event. Things drawn outside the Paint event are non-persistent and will not be included.

using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
                               pictureBox1.ClientSize.Height))
{
   pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
   bmp.Save(yourfilename, ImageFormat.Png);
}

请注意:在我的测试形式 PicureBox 正坐在一个自动滚屏面板pan_PBscroll 里面。该图片显示的是像素1:1,因此,附带照片的加载,比小组,表格甚至是屏幕更大。因此,要夹到实际可见的部分我不能使用 pictureBox1.ClientSize pictureBox1.ClientRectangle ,但使用的尺寸那面板。这很可能适用于你。

Note: On my test Form the PicureBox is sitting inside an AutoScroll Panel pan_PBscroll. The PictureBox is displaying pixels 1:1 and is therefore, with a photograph loaded, much bigger than the Panel, the Form or even the Screen. So to clip to the actually visible parts I could not use the pictureBox1.ClientSize and pictureBox1.ClientRectangle but used the dimensions of that Panel. This may well apply to you, too.

我不知道你的时序问题。但既然你提到 CopyFromScreen 这里有一些差异:

I'm not sure about your timing issues. But since you mentioned CopyFromScreen here are a few differences:


  • CopyFromScreen 使得1:每个屏幕像素的1份

  • 这包括非持久图纸和排除任何遮盖或隐藏

  • Control.DrawToBitmap 使得控制绘制自身到一个位图,就像它在绘制自身油漆

  • 这不包括任何不属于控制,但包括其Controls集合中的所有成员

  • 这也排除了非持久性的图纸,但包括控制的全尺寸,不管它符合形式或屏幕或不上,这是否隐藏或覆盖或没有。

  • 对于活跃的滚动条仅可见部分复制控制。要复制所有你需要暂时调整其大小。然后你就可以得到一个列表框组成的完整图像,即使有一千个项目。

  • CopyFromScreen makes a 1:1 copy of each screen pixel
  • This includes non-persistent drawings and excludes anything covered or hidden
  • Control.DrawToBitmap makes the Control draw itself onto a Bitmap, just as it draws itself during Paint
  • This excludes anything that doesn't belong to the Control but includes all members of its Controls collection
  • This also excludes non-persistent drawings but includes the full Size of the Control, whether it fits on the Form or Screen or not and whether it is hidden or covered or not.
  • For Controls with active Scrollbars only the visible parts are copied. To copy all you need to resize it temporarily. Then you can get a complete image of a listbox even if it has a thousand items..

这篇关于我怎样才能捕捉为位图什么只一个PictureBox显示的是,不使用"从屏幕&QUOT复制;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆