Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Saving picture to SQL (PictureBox) GDI Error

Saving picture to SQL (PictureBox) GDI Error

Scheduled Pinned Locked Moved C#
databasehelpwinformsgraphicsdata-structures
27 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    Hi, GDI+ has the tendency to name every problem happening in Image.Save() a "generic problem occured in GDI+". It could be a full disk, a locked file, whatever. In your case the MemoryStream can hardly fail (unless you are near an out-of-memory situation), but your image itself could be invalid (not sure how you would get PictureBox accept an invalid image, unless you first assign it to PictureBox.Image, and then do something to it. :)

    Luc Pattyn [Forum Guidelines] [My Articles]


    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #9

    Well I use a OpenFileDialog to select a file and set the pictureBox.Image. Then I use the above code when you click the Save button to get the bytes and insert it into a column in a database... but it never even gets to that. I tried this and it works (not very well for large images over VPN):

        byte\[\] GetPicData()
        {
            MemoryStream ms = new MemoryStream();
            Bitmap bmNew = new Bitmap(pictureBoxPicture.Width, pictureBoxPicture.Height, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bmNew);
            g.DrawImage(pictureBoxPicture.Image, new Point(0, 0));
            g.Dispose();
    
            bmNew.Save(ms, ImageFormat.Png);
    
            byte\[\] invPic = ms.ToArray();
            ms.Close();
            return invPic;
        }
    
    L 1 Reply Last reply
    0
    • L Lost User

      Do you think there is another way of getting the image from the picture box and saving it to a sql database?

      X Offline
      X Offline
      Xmen Real
      wrote on last edited by
      #10

      try this

      new Bitmap(pictureBoxPicture.Image).Save(ms, ImageFormat.Png);

      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

      ----------------------------------------------- 128 bit encrypted signature, crack if you can

      L 1 Reply Last reply
      0
      • L Lost User

        Well I use a OpenFileDialog to select a file and set the pictureBox.Image. Then I use the above code when you click the Save button to get the bytes and insert it into a column in a database... but it never even gets to that. I tried this and it works (not very well for large images over VPN):

            byte\[\] GetPicData()
            {
                MemoryStream ms = new MemoryStream();
                Bitmap bmNew = new Bitmap(pictureBoxPicture.Width, pictureBoxPicture.Height, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bmNew);
                g.DrawImage(pictureBoxPicture.Image, new Point(0, 0));
                g.Dispose();
        
                bmNew.Save(ms, ImageFormat.Png);
        
                byte\[\] invPic = ms.ToArray();
                ms.Close();
                return invPic;
            }
        
        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #11

        Hi, that code looks OK. BTW: The PictureBox serves no purpose except it gives some size. If your image happens to be zero*zero pixels, it could still work, whereas maybe PictureBox.Image (and hence the original code) would not work, I don't know. I avoid PictureBoxes, they tend to cause more trouble than what they are worth. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        L X 2 Replies Last reply
        0
        • L Luc Pattyn

          Hi, that code looks OK. BTW: The PictureBox serves no purpose except it gives some size. If your image happens to be zero*zero pixels, it could still work, whereas maybe PictureBox.Image (and hence the original code) would not work, I don't know. I avoid PictureBoxes, they tend to cause more trouble than what they are worth. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #12

          What do you do to display an image? Do you draw the image on the form?

          L 1 Reply Last reply
          0
          • X Xmen Real

            try this

            new Bitmap(pictureBoxPicture.Image).Save(ms, ImageFormat.Png);

            TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

            ----------------------------------------------- 128 bit encrypted signature, crack if you can

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #13

            Works the same but with less code and looks better. You know if I am saving it as a png file do I have to export it from the database as a png?

            X 1 Reply Last reply
            0
            • L Lost User

              Works the same but with less code and looks better. You know if I am saving it as a png file do I have to export it from the database as a png?

              X Offline
              X Offline
              Xmen Real
              wrote on last edited by
              #14

              Jacob Dixon wrote:

              Works the same but with less code and looks better.

              you mean no error now ?

              Jacob Dixon wrote:

              You know if I am saving it as a png file do I have to export it from the database as a png?

              yes...png is best format.

              TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

              ----------------------------------------------- 128 bit encrypted signature, crack if you can

              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, that code looks OK. BTW: The PictureBox serves no purpose except it gives some size. If your image happens to be zero*zero pixels, it could still work, whereas maybe PictureBox.Image (and hence the original code) would not work, I don't know. I avoid PictureBoxes, they tend to cause more trouble than what they are worth. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                X Offline
                X Offline
                Xmen Real
                wrote on last edited by
                #15

                Luc Pattyn wrote:

                I avoid PictureBoxes, they tend to cause more trouble than what they are worth.

                like ? I never got any problem with PB. I use BackgroundImage instead.

                TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                ----------------------------------------------- 128 bit encrypted signature, crack if you can

                L 1 Reply Last reply
                0
                • X Xmen Real

                  Luc Pattyn wrote:

                  I avoid PictureBoxes, they tend to cause more trouble than what they are worth.

                  like ? I never got any problem with PB. I use BackgroundImage instead.

                  TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                  ----------------------------------------------- 128 bit encrypted signature, crack if you can

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #16

                  Hi, I paint my images myself, typically onto a Panel. That avoids problems such as: - I want my pictures to preserve aspect ratio while being rescaled - I want to select pixels with the mouse, even when the image has been rescaled - I want to be able to draw a rubber band, even when the image has been rescaled etc. All of these are either impossible or much harder than they deserve to be when using a PB. The one thing I would consider using it for is for displaying an animated image (multiTIFF and the like). :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  X 1 Reply Last reply
                  0
                  • L Lost User

                    What do you do to display an image? Do you draw the image on the form?

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #17

                    I tend to paint it myself on a Panel. Also see reply below. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Hi, I paint my images myself, typically onto a Panel. That avoids problems such as: - I want my pictures to preserve aspect ratio while being rescaled - I want to select pixels with the mouse, even when the image has been rescaled - I want to be able to draw a rubber band, even when the image has been rescaled etc. All of these are either impossible or much harder than they deserve to be when using a PB. The one thing I would consider using it for is for displaying an animated image (multiTIFF and the like). :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                      X Offline
                      X Offline
                      Xmen Real
                      wrote on last edited by
                      #18

                      well, If you're a pro programmer then you should use the appropriate control. PB is for pictures and panel for containing other controls. Am I wrong...? All the operations you're doing with Panel can be done with PB but since Panel is more powerful in itself, its not good to compare with PB. Its like byte(PB) and int(Panel).

                      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                      ----------------------------------------------- 128 bit encrypted signature, crack if you can

                      L L 2 Replies Last reply
                      0
                      • X Xmen Real

                        well, If you're a pro programmer then you should use the appropriate control. PB is for pictures and panel for containing other controls. Am I wrong...? All the operations you're doing with Panel can be done with PB but since Panel is more powerful in itself, its not good to compare with PB. Its like byte(PB) and int(Panel).

                        TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                        ----------------------------------------------- 128 bit encrypted signature, crack if you can

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #19

                        I agree with both of you. I am not a Pro Programmer (yet :-) lol), but I see the advantages of both. One, I know how to use a picturebox (well I thougth I did before this issue, but it seems to be a little bug), and I do not know how to paint pictures at ALL. I would like to learn it, and I think I might try the panel way to just test it out. Not really sure where to begin so I will google painting image on panel

                        L 1 Reply Last reply
                        0
                        • L Lost User

                          I agree with both of you. I am not a Pro Programmer (yet :-) lol), but I see the advantages of both. One, I know how to use a picturebox (well I thougth I did before this issue, but it seems to be a little bug), and I do not know how to paint pictures at ALL. I would like to learn it, and I think I might try the panel way to just test it out. Not really sure where to begin so I will google painting image on panel

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #20

                          Hi, there are lots of articles that demonstrate how to paint things. My Sokoban article is a simple example, the playing board actually is a Panel. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                          X L 2 Replies Last reply
                          0
                          • X Xmen Real

                            well, If you're a pro programmer then you should use the appropriate control. PB is for pictures and panel for containing other controls. Am I wrong...? All the operations you're doing with Panel can be done with PB but since Panel is more powerful in itself, its not good to compare with PB. Its like byte(PB) and int(Panel).

                            TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                            ----------------------------------------------- 128 bit encrypted signature, crack if you can

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #21

                            Xmen wrote:

                            you should use the appropriate control.

                            Yes, when available.

                            Xmen wrote:

                            PB is for pictures and panel for containing other controls.

                            A PB can hold other controls as well. So it is doing (or trying to do) more than what a Panel does. When not containing other Controls, a Panel is just like a piece of canvas, ideal for painting and drawing whatever you want. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                            X 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Hi, there are lots of articles that demonstrate how to paint things. My Sokoban article is a simple example, the playing board actually is a Panel. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                              X Offline
                              X Offline
                              Xmen Real
                              wrote on last edited by
                              #22

                              Luc Pattyn wrote:

                              My Sokoban article is a simple example

                              nice work man, you got ma 5 ;)

                              TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                              ----------------------------------------------- 128 bit encrypted signature, crack if you can

                              1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Hi, there are lots of articles that demonstrate how to paint things. My Sokoban article is a simple example, the playing board actually is a Panel. :)

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #23

                                Now I take it you can not only write an image to a panel, but you can retrieve an image from it?

                                X L 2 Replies Last reply
                                0
                                • L Luc Pattyn

                                  Xmen wrote:

                                  you should use the appropriate control.

                                  Yes, when available.

                                  Xmen wrote:

                                  PB is for pictures and panel for containing other controls.

                                  A PB can hold other controls as well. So it is doing (or trying to do) more than what a Panel does. When not containing other Controls, a Panel is just like a piece of canvas, ideal for painting and drawing whatever you want. :)

                                  Luc Pattyn [Forum Guidelines] [My Articles]


                                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                                  X Offline
                                  X Offline
                                  Xmen Real
                                  wrote on last edited by
                                  #24

                                  Luc Pattyn wrote:

                                  A PB can hold other controls as well.

                                  aye aye but not like panel...you have to add programmatically.

                                  Luc Pattyn wrote:

                                  When not containing other Controls, a Panel is just like a piece of canvas, ideal for painting and drawing whatever you want.

                                  aha but no support for gif ;) but no worries, this is end of discussion

                                  TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                                  ----------------------------------------------- 128 bit encrypted signature, crack if you can

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    Now I take it you can not only write an image to a panel, but you can retrieve an image from it?

                                    X Offline
                                    X Offline
                                    Xmen Real
                                    wrote on last edited by
                                    #25

                                    simple drawing method Draw image and set it to global variable. Then in OnPaint() draw that image. In this way, you can have image anytime. But if you just want to set a single image from file or stream, I'll suggest you to use BackgoundImage property. No coding required. Plus you can use styles, Tiles, Center, Zoom...etc

                                    TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                                    ----------------------------------------------- 128 bit encrypted signature, crack if you can

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      Now I take it you can not only write an image to a panel, but you can retrieve an image from it?

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #26

                                      Hi Jacob, first of all, if you have code to paint/draw something (that is a "Paint handler" which is called automatically with a "Graphics" parameter), you have all the information and the same code could be used to draw onto another Graphics object, e.g. one you obtain from Bitmap.CreateGraphics(). Furthermore there is a Control.DrawToBitmap() method available for all Controls, it creates a bitmap showing the Control the way it looks on screen. Since a Panel does not draw anything by itself, Panel.DrawToBitmap() paints your own content to the bitmap, which is quite handy. :)

                                      Luc Pattyn [Forum Guidelines] [My Articles]


                                      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                                      L 1 Reply Last reply
                                      0
                                      • L Luc Pattyn

                                        Hi Jacob, first of all, if you have code to paint/draw something (that is a "Paint handler" which is called automatically with a "Graphics" parameter), you have all the information and the same code could be used to draw onto another Graphics object, e.g. one you obtain from Bitmap.CreateGraphics(). Furthermore there is a Control.DrawToBitmap() method available for all Controls, it creates a bitmap showing the Control the way it looks on screen. Since a Panel does not draw anything by itself, Panel.DrawToBitmap() paints your own content to the bitmap, which is quite handy. :)

                                        Luc Pattyn [Forum Guidelines] [My Articles]


                                        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #27

                                        ohh.. ok that sounds like something I might be able to use to show a picture of our floor plan. Thanks for all of your guys help

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups