Hi,

I have picture box with some image int it. I want to draw a line on the image. But when code executes, line is there but image is gone.
Here is the code:

bitmap bmp=new bitmap(pb.width,pb.height);
graphics g=new graphics.fromImage(bmp);
pen p=new pen(color.black,5);
g.drawLine(p,0,0,10,10);

Line is there, but underlaying image is gone. What should I do?

Pete

Dani AI

Generated

Short version: replaced the original image with a brand‑new empty Bitmap, drew a line on that blank canvas, and then showed it — so the picture "disappeared." As noted, you need to either draw the overlay every time the control repaints, or compose a new display image from the original and show that. On devices where Paint/OnPaint isn’t convenient (WinMobile / .NET CF quirks), do the composition in memory and assign the result to the PictureBox.Image.

Example — draw non‑destructively onto a copy and replace the displayed image (dispose the old image to avoid leaks):

// keep a master copy (do this once)
Bitmap master = new Bitmap(pictureBox.Image);

// make a working copy to draw on
Bitmap view = new Bitmap(master);
using (Graphics g = Graphics.FromImage(view))
using (var pen = new Pen(Color.Black, 5))
{
    g.DrawLine(pen, 0, 0, 10, 10);
}

// swap images (dispose previous if needed)
var old = pictureBox.Image;
pictureBox.Image = view;
old?.Dispose();

Dragging (panning) instead of scrollbars: treat the PictureBox as a viewport into master. Track mouse down/move/up, maintain an (offsetX, offsetY) and redraw only the visible rectangle from the master into a viewport bitmap:

// call this after offset changes
void UpdateViewport()
{
    var vp = new Bitmap(pictureBox.Width, pictureBox.Height);
    using (Graphics g = Graphics.FromImage(vp))
    {
        g.DrawImage(master,
            new Rectangle(0,0,vp.Width,vp.Height),                // dest
            new Rectangle(offsetX,offsetY,vp.Width,vp.Height),    // src
            GraphicsUnit.Pixel);
    }
    var old = pictureBox.Image; pictureBox.Image = vp; old?.Dispose();
}

Notes and tips: use MouseDown/Move/Up for panning (drag‑and‑drop is different; ’s point is useful but not the same as panning). Always keep an untouched master image, reuse bitmaps when possible, and dispose old bitmaps to avoid memory exhaustion on compact devices. If you can override OnPaint (desktop WinForms), draw overlays there for non‑destructive, on‑the‑fly rendering; otherwise use the memory‑composition approach shown above.

Recommended Answers

All 8 Replies

Because you need to paint it there each time the picture is painted most likely

I'm working on application for WinMobile and there isn't OnPaint method there...

Thanks, I have sorted it out.
Another question, do You have any idea how could I drag the image inside pictureBox instead of using scrollbars.

this was covered in another thread - you need to store the picture in memory, and then copy and show only in the picture box what the user is expecting to see

Do You have some example code, plz

Hello,
Please tell me the some references about c#.

Won't the drag and drop method for control work in WM?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.