locked
C#, Assign Clipboard.GetImage() to variable and upload to BLOB MySQL RRS feed

  • Question

  • Hi all,

    I'm a newbie to c# and i have a problem with converting clipboard image to variable and then upload it to blob in mysql, is anyone who did it before and will be so kind to help a little?

    I simply don't know hot to transform Image to byte (as I think its the way to upload later to mysql blob).

    Thanks,

    Jakub

    using this function:

    public System.Drawing.Image SwapClipboardImage(
            System.Drawing.Image replacementImage)
            {
                System.Drawing.Image returnImage = null;
                if (Clipboard.ContainsImage())
                {
                    returnImage = Clipboard.GetImage();
                    //Clipboard.SetImage(replacementImage);
                }
                return returnImage;
            }

    i have an eror message: 

    Cannot implicitly convert type 'System.Windows.Media.Imaging.BitmapSource' to 'System.Drawing.Image'
    • Edited by Xziomas Friday, November 14, 2014 8:51 AM
    Friday, November 14, 2014 8:28 AM

Answers

  • Your error message has nothing to do with SQL at this point. It is telling you that GetImage is of type BitmapSource and not Image which is what your returnImage variable is.  You are trying to use WPF to access the clipboard info. If you are creating a WPF app then this is fine and you shouldn't be using anything from System.Drawing. Change your variable's type to BitmapSource.

    If you are instead using Winforms then remove any references to WPF (PresentationFramework, etc) and add a using for System.Windows.Forms and your code will compile.

    Once you resolve the compilation error you're ready to tackle the problem of getting the image into the DB. For that you'll have to convert the image to a byte array. There are a couple of different ways of doing this but probably the easiest approach is to save the image to a MemoryStream using Save (if you're targeting WinForms) and then calling ToArray to get the byte array to store.

    //Not tested
    using (var stream = new MemoryStream())
    {
       //Save image to memory stream using whatever format you need
       yourImage.Save(stream, ImageFormat.?);
    
       //Store array in parameter or dataset or wherever
       yourParam.Value = stream.ToArray();
    };

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Barry Wang Monday, November 17, 2014 10:19 AM
    • Marked as answer by Barry Wang Friday, November 21, 2014 8:28 AM
    Friday, November 14, 2014 4:24 PM

All replies

  • Please change System.Windows.Clipboard class to System.Windows.Forms.Clipboard class
    Friday, November 14, 2014 1:50 PM
  • Your error message has nothing to do with SQL at this point. It is telling you that GetImage is of type BitmapSource and not Image which is what your returnImage variable is.  You are trying to use WPF to access the clipboard info. If you are creating a WPF app then this is fine and you shouldn't be using anything from System.Drawing. Change your variable's type to BitmapSource.

    If you are instead using Winforms then remove any references to WPF (PresentationFramework, etc) and add a using for System.Windows.Forms and your code will compile.

    Once you resolve the compilation error you're ready to tackle the problem of getting the image into the DB. For that you'll have to convert the image to a byte array. There are a couple of different ways of doing this but probably the easiest approach is to save the image to a MemoryStream using Save (if you're targeting WinForms) and then calling ToArray to get the byte array to store.

    //Not tested
    using (var stream = new MemoryStream())
    {
       //Save image to memory stream using whatever format you need
       yourImage.Save(stream, ImageFormat.?);
    
       //Store array in parameter or dataset or wherever
       yourParam.Value = stream.ToArray();
    };

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Barry Wang Monday, November 17, 2014 10:19 AM
    • Marked as answer by Barry Wang Friday, November 21, 2014 8:28 AM
    Friday, November 14, 2014 4:24 PM