6

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?

EDIT: Here is what I want to do 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

13driver
  • 237
  • 2
  • 5
  • 10
  • @Mohsen Afshin No print on screen As in just show the image – 13driver Oct 27 '12 at 20:17
  • Do you want to 1) Create a screenshot of your application and display it on the desktop (like a wallpaper) or 2) Create a screenshot of your desktop and display it in your application. – Alex Essilfie Oct 27 '12 at 20:43
  • I want to take screenshots of my main monitor ( in this specific way) and to view it … that’s it . I think I have all the data I need to assemble a picture of it. dont i ? – 13driver Oct 27 '12 at 20:59

5 Answers5

9

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);
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • I know there are other ways of taking screenshots, but I need to have all the dimensions and colors of the screen for something else So I have to use this method – 13driver Oct 27 '12 at 20:33
  • @maikl: I guess I don't understand what you're trying to do. You want to display an image on the screen, but wait, no, we can't show anything on the screen? – icktoofay Oct 27 '12 at 20:35
  • `the type or namespace Form could not be found` – john k Mar 16 '23 at 03:05
4

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

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • All that does is copy the image to itself at (60, 10), which does not appear to be what they want to do. – icktoofay Oct 27 '12 at 20:36
  • @icktoofay Sorry, I could not understand your comment. May you please explain more? :) – Picrofo Software Oct 27 '12 at 20:37
  • Nope , it isn’t displaying the image . No error messages no nothing .All it does is copy – 13driver Oct 27 '12 at 20:43
  • @maikl Based on your code, I've only changed the way you get the graphics from. You used to get it from the event, now you can get it within the bitmap image. Perhaps there's something wrong with your code. I'm sorry I could not help this time :( – Picrofo Software Oct 27 '12 at 20:45
  • @Maikl Oh, I almost forgot. I've also changed `print (bmp,)` to `print(bitamp)`. Have a great day and sorry again :( – Picrofo Software Oct 27 '12 at 20:46
  • @icktoofay Not sure if this is any help but here is what I want to do http://msdn.microsoft.com/en-us/library/8tda2c3c.aspx – 13driver Oct 27 '12 at 20:50
  • @Maikl Thank you for providing the information. I'll do my best to achieve this! :) – Picrofo Software Oct 27 '12 at 20:51
  • 1
    @PicrofoEGY Thanks a whole lot =) PS: what do you mean by “you can do this within the designer too!”? (line 3) – 13driver Oct 27 '12 at 21:38
  • @maikl I meant that you can set the event within the designer if this is a Windows Forms application as in [this screenshot](http://i.stack.imgur.com/nKf0j.png). – Picrofo Software Oct 27 '12 at 21:41
  • @maikl I'm glad I could help, you may always mark a post as an answer to your question. Have a great day :) – Picrofo Software Oct 27 '12 at 21:42
  • I know this is a long time after all of these comments came about, but it's worth pointing out that in your `print()` function you can clean it up with the following: `using (Graphics graphicsObj = e.Graphics) { graphicsObj.DrawImage(BM, 60, 10); }`. That way you don't actually have to call `Dispose()` yourself -- `Dispose()` will get called whenever that code block finishes and you can't accidentally forget it. (Sorry for the code block in a comment; I didn't think it was worth a new post.) – Ari Roth Sep 05 '17 at 04:12
1

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);
Ramgy Borja
  • 2,330
  • 2
  • 19
  • 40
0

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

Every example I tried didn't work. here's my code that worked for me.

using System.Drawing;
using System.Windows.Forms;

//create your bitmap

 Form1 form1 = new Form1(bitmap);


public class Form1 : System.Windows.Forms.Form
{
    Bitmap bitmap;
    public Form1(Bitmap bitmap)
    {
        this.bitmap = bitmap;
        this.ClientSize = new System.Drawing.Size(this.bitmap.Width, this.bitmap.Height);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        Application.Run(this);
    }
   
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Graphics gForm = e.Graphics;
        gForm.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
        bitmap.Dispose();
    }
}
john k
  • 6,268
  • 4
  • 55
  • 59