4

我试图在我的主监视器上打印(显示在屏幕上)屏幕截图,我想我已经拥有了所有必要的变量来实现这一点,但我不知道如何通过“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:这是我第一次使用该网站,所以请原谅我可能犯的任何愚蠢的错误

4

4 回答 4

6

The simplest way would be to use a Windows Forms PictureBox inside a Form.

For example:

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);
于 2012-10-27T20:23:23.270 回答
4

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

尝试这个

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);
}

谢谢,
我希望你觉得这有帮助:)

于 2012-10-27T20:34:05.997 回答
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);
于 2017-09-05T03:21:39.613 回答
0

我有一个很好的解决方案,但你需要计算你的东西的位置和尺寸。不幸的是,我还没有弄清楚如何在屏幕上绘制 img。

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 毫秒处做了等待部分,这样点就不会闪烁。谢天谢地,它是一个控制台应用程序,所以它不会冻结。

Draw Directly To Screen中找到了这个解决方案。

于 2020-04-11T07:27:14.633 回答