How do I use a System.Drawing.Bitmap as an image source on a report (.rdlc)? RRS feed

  • Question

  • Hi guys,

    I have an image stored in a System.Drawing.Bitmap object, and would liek to display that image in a report.

    The following web site...

    "To use a database image, you can specify the database field that contains the image as long as the MIME type is JPG, PNG, or BMP. If the image is stored as a binary object, you must write an expression that converts the image to a supported type."

    So, my question is, how do I convert my System.Drawing.Bitmap object to 'a supported type'?  What types are supported?

    I can happily save the Bitmap to disk as a .PNG or .JPG or whatever, and then use it on my report, but I don't want to do it that way.  I want to use the report's image source of 'Database', not 'External'.

    Surely this can't be as hard as it seems?

    Many thanks in advance,
    Thursday, June 18, 2009 9:26 AM


  • Never mind, I found the solution on my own.

    In case anyone wants to know, you need to convert the Bitmap to a byte array, which you can do as follows: 
    System.Drawing.Bitmap b = <your Bitmap object>;
    byte[] bytes = new byte[]; using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { b.Save(stream, System.Drawing.Imaging.ImageFormat.Png); bytes = stream.ToArray(); }

    You can then use the 'bytes' variable, in this case, on your report as the image source.  In this case I've used the PNG image format, so you'd need to remember to set the MIME type correctly.

    • Marked as answer by madskunk Thursday, June 18, 2009 10:30 AM
    Thursday, June 18, 2009 10:30 AM