Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a Bitmap image on the screen

Tags:

c#

bitmap

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

like image 377
13driver Avatar asked Dec 21 '22 14:12

13driver


2 Answers

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);
like image 109
icktoofay Avatar answered Feb 23 '23 00:02

icktoofay


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

like image 38
Picrofo Software Avatar answered Feb 22 '23 23:02

Picrofo Software