【问题标题】:Draw a Bitmap image on the screen在屏幕上绘制位图图像
【发布时间】:2012-10-27 20:00:25
【问题描述】:

我正在尝试在我的主显示器上打印(显示在屏幕上)屏幕截图,我想我已经拥有了实现这一目标的所有必要变量,但我不知道如何通过“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 屏幕上没有打印 只显示图像
  • 您想要 1) 创建您的应用程序的屏幕截图并将其显示在桌面上(如墙纸)还是 2) 创建您的桌面屏幕截图并将其显示在您的应用程序中。
  • 我想截取我的主显示器的屏幕截图(以这种特定方式)并查看它……就是这样。我想我拥有组装一张照片所需的所有数据。不是吗?

标签: c# bitmap


【解决方案1】:

最简单的方法是在Form 中使用Windows 窗体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);

【讨论】:

  • 我知道还有其他的截图方法,但是我需要有屏幕的所有尺寸和颜色才能做别的事情所以我必须使用这种方法
  • @maikl:我想我不明白你在做什么。您想在屏幕上显示图像,但是等等,不,我们不能在屏幕上显示任何东西?
【解决方案2】:

您需要在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 抱歉,我无法理解您的评论。你能解释一下吗? :)
  • 不,它不显示图像。没有错误消息没有任何东西。它所做的只是复制
  • @maikl 根据您的代码,我只是更改了您从中获取图形的方式。您以前从事件中获取它,现在您可以在位图图像中获取它。也许你的代码有问题。很抱歉这次我帮不上忙 :(
  • @PicrofoEGY 非常感谢 =) PS:“你也可以在设计器中做到这一点!”是什么意思? (第 3 行)
【解决方案3】:

让它变得简单。

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

【讨论】:

    【解决方案4】:

    我有一个很好的解决方案,但你需要计算你的东西的位置和尺寸。不幸的是,我还没有弄清楚如何在屏幕上绘制一个 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 找到这个解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2013-09-13
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多