在屏幕上绘制位图图像

时间:2012-10-27 20:00:25

标签: c# bitmap

我试图在我的主显示器上打印(在屏幕上显示)截图,我想我已经拥有了所有必要的变量来实现这一点,但我不知道如何通过“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 个答案:

答案 0 :(得分:6)

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

例如:

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

答案 1 :(得分:2)

您需要在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);
}

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

答案 2 :(得分:0)

让它变得简单。

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

答案 3 :(得分: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找到了这个解决方案。