Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 732397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:08:15+00:00 2026-05-14T07:08:15+00:00

I have a pictureBox on a Windows Form. I do the following to load

  • 0

I have a pictureBox on a Windows Form.

I do the following to load a PNG file into it.

Bitmap bm = (Bitmap)Image.FromFile("Image.PNG", true);
Bitmap tmp;

public Form1() {
    InitializeComponent();
    this.tmp = new Bitmap(bm.Width, bm.Height);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e) {
    e.Graphics.DrawImage(this.bm, new Rectangle(0, 0, tmp.Width, tmp.Height), 0, 0, tmp.Width, tmp.Height, GraphicsUnit.Pixel);
}

However, I need to draw things on the image and then have the result displayed again. Drawing rectangles can only be done via the Graphics class.

I’d need to draw the needed rectangles on the image, make it an instance of the Image class again and save that to this.bm

I can add a button that executes this.pictureBox1.Refresh();, forcing the pictureBox to be painted again, but I can’t cast Graphics to Image. Because of that, I can’t save the edits to the this.bm bitmap.

That’s my problem, and I see no way out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:08:15+00:00Added an answer on May 14, 2026 at 7:08 am

    What you need to do is use the Graphics.FromImage method, which will allow you to draw directly on the image instead of the temporary Graphics object create from within the Paint method:

    using (Graphics g = Graphics.FromImage(this.bm))
    {
        g.DrawRectangle(...);
    }
    

    Do this instead of (or in addition to) hooking the Paint method of the PictureBox. This way, you won’t need to use a temporary image or Graphics object at all, and when you’re finished modifying the original bitmap (this.bm) then you can invoke pictureBox1.Refresh to force the image to be re-displayed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a label using the following code: public static System.Windows.Forms.PictureBox pc =
I have windows form. I have put one loading image in PictureBox When I
I have a PictureBox on my Windows Forms application. I load a picture in
I have a Windows Form Application in Visual Studio 2008 - PictureBox.Refesh() method causes
I have following problem. I want to make some graphics in c# windows form.
On the Windows Form application I have a Lamp image (a black and white
I have a GUI window form. I want to set image to pictureBox and
i have a PictureBox in my Windows Form application and i am in the
I a have a PictureBox with a picture in a Windows Form application in
I have 1 Windows Form with 1 pictureBox, 1 textBox, and 3 buttons: LoadFromFile,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.