如何绘制截图“预览”窗口?

时间:2010-12-01 15:00:36

标签: c# winforms drawing screenshot

我有一个Winforms应用程序,用户可以使用它来获取基于区域的屏幕截图。我想要一个小预览窗格,但我不知道该怎么做。到目前为止,我尝试在鼠标移动时重新创建一个位图,它的方式太迟了,无法使用。所以我想如果我使用预定义的图像(整个屏幕的屏幕截图)并根据鼠标位置将其移动到图片框中,以便您放大屏幕的外观(选择您想要的精确像素)截图更简单)。我不知道如何实现这一点,我也很擅长绘画所以我会告诉你我现在拥有的东西。

private void falseDesktop_MouseMove(object sender, MouseEventArgs e)
    {
        zoomBox.Image = showZoomBox(e.Location);
        zoomBox.Invalidate();
    }

private Image showZoomBox(Point curLocation)
        {
            int x = 0;
            int y = 0;
            if (curLocation.X - 12 <= 0)
            {
                x = curLocation.X - 12;
            }
            else
            {
                x = curLocation.X;
            }

            if (curLocation.Y - 11 <= 0)
            {
                y = curLocation.Y - 11;
            }
            else
            {
                y = curLocation.Y;
            }

            Point start = new Point(curLocation.X - 12, curLocation.Y - 11);
            Size size = new Size(24, 22);
            Rectangle rect = new Rectangle(start, size);
            Image selection = cropImage(falseDesktop.Image, rect);
            return selection;
        }

private static Image cropImage(Image img, Rectangle cropArea)
    {
        if (cropArea.Width != 0 && cropArea.Height != 0)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
            bmpImage.Dispose();
            return (Image)(bmpCrop);
        }
        return null;
    }

编辑:

这是一个像要求的模拟:

这个图像的黑色部分是一个面板,当然文本是一个标签,你看到图像的区域(堆栈溢出)将是图片框(称为zoomBox),zoomBox顶部的线条将是一个引导和2条线相交的位置是鼠标位置。希望这是一个更好的帮助,以帮助您了解我的问题。

alt text

可能有助于解释我的问题的另一件事是表单实际上用“虚假桌面”填满整个屏幕。当按下“printscreen”时,它是一个覆盖整个屏幕的图片框,其中包含桌面的屏幕截图。所以我希望这个小“预览窗格”基本上是鼠标所在位置的放大位置。

2 个答案:

答案 0 :(得分:2)

这有点滞后,但值得一试:

这是一个单独文件中的WInForms应用程序,显示了“实时”缩放的工作原理。它不会画出由你决定的十字线等。

关键部分:

  • _scale
  • PictureBoxSizeMode.StretchImage

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;

static class Program {
[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}
}

public class Form1 : Form {
private Bitmap _myImage = new Bitmap(@"C:\Users\Public\Pictures\Sample   Pictures\LightHouse.jpg");
private int _scale = 10; // keep this < 15

private PictureBox pboxMain;
private PictureBox pboxZoom;
private System.ComponentModel.IContainer components;

public Form1() {
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
    pboxMain.Image = _myImage;
}

private void pboxMain_MouseMove(object sender, MouseEventArgs e) {
    try {
        Rectangle rc = new Rectangle(
            new Point(e.X - _scale, e.Y - _scale),
            new Size(_scale * 2, _scale * 2));
        pboxZoom.Image = _myImage.Clone(rc, PixelFormat.DontCare);
    }
    catch (OutOfMemoryException  ex) {/* ignore... */}
}

protected override void Dispose(bool disposing) {
    if (disposing && (components != null)) {
        components.Dispose();
    }
    base.Dispose(disposing);
}

private void InitializeComponent() {
    this.pboxMain = new PictureBox();
    this.pboxZoom = new PictureBox();
    ((System.ComponentModel.ISupportInitialize)(this.pboxMain)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.pboxZoom)).BeginInit();
    this.SuspendLayout();

    this.pboxMain.Dock = DockStyle.Fill;
    this.pboxMain.Location = new System.Drawing.Point(0, 0);
    this.pboxMain.Name = "pboxMain";
    this.pboxMain.Size = new System.Drawing.Size(767, 435);
    this.pboxMain.TabIndex = 0;
    this.pboxMain.TabStop = false;
    this.pboxMain.MouseMove += new MouseEventHandler(this.pboxMain_MouseMove);

    this.pboxZoom.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))),
  ((int)(((byte)(255)))), ((int)(((byte)(192)))));
    this.pboxZoom.BorderStyle = BorderStyle.FixedSingle;
    this.pboxZoom.Location = new System.Drawing.Point(12, 12);
    this.pboxZoom.Name = "pboxZoom";
    this.pboxZoom.Size = new System.Drawing.Size(106, 83);
    this.pboxZoom.SizeMode = PictureBoxSizeMode.StretchImage;
    this.pboxZoom.TabIndex = 1;
    this.pboxZoom.TabStop = false;

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(767, 435);
    this.Controls.Add(this.pboxZoom);
    this.Controls.Add(this.pboxMain);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    ((System.ComponentModel.ISupportInitialize)(this.pboxMain)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.pboxZoom)).EndInit();
    this.ResumeLayout(false);
}
}

答案 1 :(得分:0)

这应该会有很大的帮助TeboScreen: Basic C# Screen Capture
为什么重新发明轮子: - )