在form1构造函数
中public Form1()
{
InitializeComponent();
BackColor = Color.LightGreen;
TransparencyKey = Color.LightGreen;
this.TopMost = true;
this.Location = new Point(0, 0);
timer1.Enabled = true;
}
然后:
private void Cap()
{
countImages++;
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
Bitmap bmp = new Bitmap(img);
Bitmap source = new Bitmap(this.Width, this.Height);
Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
Bitmap CroppedImage = CropImage(source, section);
CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);
source.Dispose();
}
和
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}
我保存到硬盘的位图:
CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);
他们都是黑人。 我想从图像裁剪这部分并将其保存到硬盘。
屏幕截图示例以及我要从中裁剪的内容: 我用红色圆圈标记透明form1,这就是我想要裁剪form1的部分并将其保存为form1部分的裁剪图像:
我先试了这个:
private void CaptureDesktop()
{
countImages++;
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
Bitmap bmp = new Bitmap(img);
Bitmap source = new Bitmap(this.Width, this.Height);
Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
Bitmap CroppedImage = CropImage(bmp, section);
CroppedImage.Save("c:\\temp\\desktopscreens\\" + countImages + ".gif", ImageFormat.Gif);
}
但是我得到了这张裁剪后的图片:图片中奇怪的图片?
然后我尝试了这段代码:
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(this.Location.X,
this.Location.Y,
0, 0,
this.Size,
CopyPixelOperation.SourceCopy);
}
Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
CroppedImage.Save("c:\\temp\\desktopscreens\\1.bmp", ImageFormat.Bmp);
}
但结果是:
不是我想要的。我只想从整个图像中裁剪form1部分。
答案 0 :(得分:2)
你mView = (CustomView) findViewById(R.id.custom_view);
这会创建一个黑色图像。
然后你继续裁剪它。这仍然是一个黑色的形象。
然后你继续保存它。这仍然是一个黑色的形象。
也许您打算裁剪Bitmap source = new Bitmap(this.Width, this.Height);
或Bitmap bmp
,而不是Image img
。我不知道Bitmap source
是什么意思。
答案 1 :(得分:1)
试试这个(来自Capture the screen shot using .NET的一些代码)
private void Cap()
{
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(this.Location.X,
this.Location.Y,
0, 0,
this.Size,
CopyPixelOperation.SourceCopy);
}
Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
CroppedImage.Save("c:\\temp\\screens\1.bmp", ImageFormat.Bmp);
}
}
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}