在屏幕上绘制位图图像

6
我正在尝试在我的主监视器上打印(屏幕显示)屏幕截图,我认为我已经有了所有必要的变量来实现这一点,但是我不知道如何绕过“PaintEventArgs”。我应该发送什么?我该如何做到这一点?
编辑:这就是我想做的http://msdn.microsoft.com/en-us/library/8tda2c3c.aspx
static void Main(string[] args)
{
    Rectangle rect = Screen.PrimaryScreen.Bounds;
    int color = Screen.PrimaryScreen.BitsPerPixel;
    PixelFormat pf;
    pf = PixelFormat.Format32bppArgb;           
    Bitmap BM= new Bitmap(rect.Width, rect.Height, pf);
    Graphics g = Graphics.FromImage(BM);
    g.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
    Bitmap bitamp = new Bitmap(BM);
    print (bmp,) // what now?

}

private static void print(Bitmap BM, PaintEventArgs e)
{
    Graphics graphicsObj = e.Graphics; // or "Bitmap bitmap = new Bitmap("Grapes.jpg");"
    graphicsObj.DrawImage(BM, 60 ,10); // or "e.Graphics.DrawImage(bitmap, 60, 10);"
    graphicsObj.Dispose();
}

PS:这是我第一次使用该网站,所以请原谅我可能犯的任何新手错误。


@Mohsen Afshin 不要在屏幕上打印 只需显示图像 - 13driver
你想要做什么?1)创建应用程序的屏幕截图并在桌面上显示(如壁纸),还是2)创建桌面的屏幕截图并在你的应用程序中显示。 - Alex Essilfie
我想以特定的方式截取我的主显示器的屏幕截图,并查看它...就这样。 我认为我已经拥有了组装它的所有数据,不是吗? - 13driver
5个回答

10

最简单的方法是在 Form 内使用 Windows Forms PictureBox

例如:

Form form = new Form();
form.Text = "Image Viewer";
PictureBox pictureBox = new PictureBox();
pictureBox.Image = YourImage;
pictureBox.Dock = DockStyle.Fill;
form.Controls.Add(pictureBox);
Application.Run(form);

我知道还有其他方法可以截屏,但是我需要屏幕的所有尺寸和颜色用于其他用途,所以我必须使用这种方法。 - 13driver
@maikl:我猜我不明白你想做什么。你想在屏幕上显示一张图片,但是等等,我们不能在屏幕上显示任何东西? - icktoofay
the type or namespace Form could not be found - john k

4

您需要在Paint事件中调用print(Bitmap, PaintEventArgs)方法。

请尝试以下操作

private void Form1_Load(object sender, EventArgs e)
{
    Paint += new PaintEventHandler(Form1_Paint); //Link the Paint event to Form1_Paint; you can do this within the designer too!
}
private void print(Bitmap BM, PaintEventArgs e)
{
    Graphics graphicsObj = e.Graphics; //Get graphics from the event
    graphicsObj.DrawImage(BM, 60, 10); // or "e.Graphics.DrawImage(bitmap, 60, 10);"
    graphicsObj.Dispose();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
     Rectangle rect = Screen.PrimaryScreen.Bounds;
     int color = Screen.PrimaryScreen.BitsPerPixel;
     PixelFormat pf;
     pf = PixelFormat.Format32bppArgb;
     Bitmap BM = new Bitmap(rect.Width, rect.Height, pf); //This is the Bitmap Image; you have not yet selected a file,
     //Bitmap BM = new Bitmap(Image.FromFile(@"D:\Resources\International\Picrofo_Logo.png"), rect.Width, rect.Height);
     Graphics g = Graphics.FromImage(BM);
     g.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
     Bitmap bitamp = new Bitmap(BM);
     print(bitamp, e);
}

谢谢,
希望这对你有帮助 :)


所有它所做的就是将图像复制到自身的(60,10)位置,这似乎不是他们想要做的。 - icktoofay
@icktoofay 抱歉,我无法理解您的评论。您能否请再解释一下呢? :) - Picrofo Software
@icktoofay 不太确定这能不能帮到你,但这是我想要做的事情 http://msdn.microsoft.com/zh-cn/library/8tda2c3c.aspx - 13driver
1
@PicrofoEGY 非常感谢 =) PS:你说的“你也可以在设计器中完成这个操作!”是什么意思?(第三行) - 13driver
@maikl 我的意思是,如果这是一个 Windows Forms 应用程序,你可以在设计器中设置事件,就像这张截图中展示的一样(http://i.stack.imgur.com/nKf0j.png)。 - Picrofo Software
显示剩余6条评论

1
让它简单明了。
    //Draw Image

    // location of you image
    Bitmap img = Properties.Resources.myImage;

    // set image 
    Image newImg = img;

    // draw a image
    e.Graphics.DrawImage(newImg, 180F, 18F, newImg.Width, newImg.Height);

0

我尝试了每个例子都不起作用。这是我的代码,对我有用。

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

//create your bitmap

 Form1 form1 = new Form1(bitmap);


public class Form1 : System.Windows.Forms.Form
{
    Bitmap bitmap;
    public Form1(Bitmap bitmap)
    {
        this.bitmap = bitmap;
        this.ClientSize = new System.Drawing.Size(this.bitmap.Width, this.bitmap.Height);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        Application.Run(this);
    }
   
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Graphics gForm = e.Graphics;
        gForm.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
        bitmap.Dispose();
    }
}

0

我有一个非常好的解决方案,但你需要计算你的元素的位置和大小。不幸的是,我还没有弄清楚如何将图像绘制到屏幕上。

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;

namespace DIRECTDRAW
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hwnd);

        static void draw(Rectangle r, Brush b, IntPtr hwnd)
        {
            using (Graphics g = Graphics.FromHdc(hwnd))
            {
                g.FillRectangle(b, r);
            }
        }

        static void Main(string[] args)
        {
            int w = 5;
            int h = 5;

            Point xy = new Point(960 - w, 540 - h);
            draw(new Rectangle(xy, new Size(w, h)), new SolidBrush(Color.White), GetDC(IntPtr.Zero));
            Thread.Sleep(1);
            Main(args);
        }
    }
}


我加入了一个循环函数,以便您的“东西”不会消失,并且我将等待部分设置为1毫秒,以使点不闪烁。幸运的是,这是一个控制台应用程序,所以它不会冻结。
直接绘制到屏幕中找到了这个解决方案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接