C# 在屏幕上绘制位图图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13103682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:36:49  来源:igfitidea点击:

Draw a Bitmap image on the screen

c#bitmap

提问by 13driver

Im trying to print (show in screen ) a screenshot on my main monitor , I think I've got all the necessary variables to make that happen but I have no clue how to get past “ PaintEventArgs” . What should I send, how should I do it?

我试图在我的主监视器上打印(在屏幕上显示)一个屏幕截图,我想我已经有了所有必要的变量来实现这一点,但我不知道如何通过“PaintEventArgs”。我应该发送什么,我该怎么做?

EDIT: Here is what I want to do http://msdn.microsoft.com/en-us/library/8tda2c3c.aspx

编辑:这是我想要做的 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: this is my first time using the site so excuse any noobish mistakes I might have made

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

采纳答案by Picrofo Software

You'll need to call print(Bitmap, PaintEventArgs)within the form Paintevent.

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

Try this

尝试这个

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

Thanks,
I hope you find this helpful :)

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

回答by icktoofay

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

最简单的方法是PictureBoxForm.

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

回答by Ramgy Borja

make it simple just.

让它变得简单而已。

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

回答by Jeremiah Jordan

I have a very good solution, but you need to calculate the position and sizing for your stuff. Unfortunately, I have not figured out how to draw an img to screen yet.

我有一个很好的解决方案,但你需要计算你的东西的位置和尺寸。不幸的是,我还没有弄清楚如何将 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);
        }
    }
}

I put in a loop function so that your "thing" doesn't disappear, and I made the wait part at 1 milisecond so that the dot doesn't flicker. Thankfully it's a console app so it won't freeze.

我加入了一个循环函数,这样你的“东西”就不会消失,我在 1 毫秒时做了等待部分,这样点就不会闪烁。值得庆幸的是,它是一个控制台应用程序,因此不会冻结。

Found this solution out from Draw Directly To Screen.

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