Draw a Bitmap image on the screen

后端 未结 4 1673
渐次进展
渐次进展 2021-01-13 15:40

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

相关标签:
4条回答
  • 2021-01-13 15:59

    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);
    
    0 讨论(0)
  • 2021-01-13 16:05

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

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

    0 讨论(0)
  • 2021-01-13 16:10

    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.

    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.

    Found this solution out from Draw Directly To Screen.

    0 讨论(0)
  • 2021-01-13 16:21

    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);
    
    0 讨论(0)
提交回复
热议问题