Click here to Skip to main content
12,552,460 members (27,380 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB GDI+ VB.NET
I have been searching here on CP for some articles and I have not found anything that has helped me. If anyone can point me in the proper direction I would greatly appreciate it.

This app that I have created uses GDI+ to draw primitive shapes on a picturebox. This works rather well. My main concern now is how do I now take that GDI+ image and save it to a clipboard? Below is a mock-up of my app.

Private m_Graphics As Graphics
Private Sub Picture1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Picture1.Paint
      m_Graphics = e.Graphics
      Draw_Primitive_Shapes_On_Screen() ' This sub routine uses m_Graphics to draw lines, circles, etc.

 End Sub
Private Sub cmdCopyToClipboard_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCopyToClipboard.Click
      Clipboard.SetImage(Picture1.Image) ' Returns Nothing
End Sub

In this mock-up in my app the Picture1.Image returns Nothing. I was always under the impression that this would give me what I have done to the PictureBox using GDI+. I have tried searching around, but I don't know what I should be focusing on. GDI+ to Image? Or PictureBox to Image?

Does this make sense? If you need any additional info just let me know.

Any help would be greatly appreciated.
Posted 26-Jul-12 5:54am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You're not painting an image to a Bitmap object. You're painting the image to the screen inside the bounds of the PictureBox control.

The Graphics class has a method, FromImage, which will return a Graphics object that can be used to draw to the Bitmap instance you create.
Dim myImage As New Bitmap(xDimension, yDimension)
Dim m_graphics As Graphics = Graphics.FromImage
Clark Kent123 26-Jul-12 15:06pm
Thanks Dave for that. I am currently running some experiments so I get a grasp of this.
Clark Kent123 26-Jul-12 16:05pm
Grr... I thought it was just me, but it seems a bunch of people who have used the GDI+ bitmap solution that you have suggested are receiving the problem that I am now getting "Parameter is not valid." This is when you call any GDI+ function after you initialize it using the FromImage using a bitmap. Hmmm, not sure what to do at this point. Back to the drawing board (pun not intended :) ).

Your advice works in theory so I will give it an accept solution, but does not get me the what I want. I appreciate the help Dave.
Dave Kreskowiak 26-Jul-12 16:20pm
"Parameter not valid"?? On what code? How big in the Bitmap you're creating?
What does the drawing code look like? How about the code this is puttin ght eBitmap on the clipboard?
Clark Kent123 26-Jul-12 16:28pm
Posted is the code that I am testing out.

Dim bmpPictureBox As New Bitmap(Me.Picture1.Width, Me.Picture1.Height)
Dim imgToBeCopied As Image
imgToBeCopied = bmpPictureBox
'Any function called from m_Graphics receives the "Parameter not valid."
'For instance this does not work m_Graphics.Clear(Color.White)

The dimensions of my PictureBox that I want the Bitmap to use is: 840x 400. Hopefully that is not too large. Maybe I can make it smaller and see what happens.
Dave Kreskowiak 26-Jul-12 16:50pm
What's with the Image garbage?? You don't need it.

Dim newBitmap As New Bitmap(...,...)
Dim g As Graphics = Graphics.FromImage(newBitmap)

BTW, this is not copying anything from the picturebox. With the code that you posted above, the drawing that you have is actually blank anyway. There's nothing to copy reguardless of what you see on the screen.
Clark Kent123 26-Jul-12 17:04pm
Oh my! It works. I copied and pasted what you put and it works. What was I doing wrong? Maybe the Image Object was making things unstable. This whole time I was thinking that you had to give it a Image Object. Never tried the bitmap by itself. Well... thanks.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Okay, here you go. I can't explain the code, since I'm not familiar with .NET and have just cobbled this together using the ode code copied from here: How to: Create a Bitmap at Run Time[^]

I have a form, with one button and one PictureBox. It's called PictureBox1a

Public Class Form1
    Private pictureBox1 As New PictureBox()
    Dim flag As New Bitmap(200, 100)
    Public Sub CreateBitmapAtRuntime()
        PictureBox1a.Size = New Size(210, 110)

        Dim flagGraphics As Graphics = Graphics.FromImage(flag)
        Dim red As Integer = 0
        Dim white As Integer = 11
        While white <= 100
            flagGraphics.FillRectangle(Brushes.Blue, 0, red, 200, 10)
            flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10)
            red += 20
            white += 20
        End While
        PictureBox1a.Image = flag
    End Sub
    Private Sub copyBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copyBtn.Click
    End Sub
End Class

Hitting Ctrl-Shift-V(paste as new image) in Gimp gives me the freshly drawn image.
Clark Kent123 26-Jul-12 16:46pm
Thanks for the help. I am still receiving the same error I have posted under Dave's solution ("Parameter is not valid"). It seems GDI+ becomes unstable when you pair it with a Bitmap Object. I even tried reducing the image size to see if that will help, but I get the same error.

What I plan on doing is using the code you posted and placing it in a separate solution and see if I get the same results.

Thanks though.
enhzflep 26-Jul-12 16:51pm
No worries mate.
This code is the entire code of a brand-new project (VS2010). It does work here. Without seeing a greater portion of yours I'm not in a position to guess - though as mentioned I'm almost up to the level of useless in .NET, so probably couldn't help if I saw more any way. :laughs:

Cheers. :)
Clark Kent123 26-Jul-12 17:06pm
Yeah your solution worked as well. I ended up adding some more variables that were unnecessary and became unstable. Once I removed them it worked just fine. Well, thanks for the help.
enhzflep 26-Jul-12 19:10pm
Pleasure Superman. :p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161021.1 | Last Updated 26 Jul 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100