How can I return a bitmap image from a custom assembly to an SSRS report?

  • I am using the QRCode4CS class (http://qrcode4cs.codeplex.com/releases/view/74015) to generate QR codes.

    I can use the following code to successfully return a bitmap image to a picturebox in a Windows Form Application.

    public class CreateQRCodeClass

    {

    public static Image CreateQRCodeImage(string inputString)

    {

    QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));

    qrcode.Make();

    Image canvas = new Bitmap(86, 86);

    Graphics artist = Graphics.FromImage(canvas);

    artist.Clear(Color.White);

    for (int row = 0; row < qrcode.GetModuleCount(); row++)

    {

    for (int col = 0; col < qrcode.GetModuleCount(); col++)

    {

    bool isDark = qrcode.IsDark(row, col);

    if (isDark == true)

    {

    artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    else

    {

    artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    }

    }

    artist.FillRectangle(Brushes.White, 0, 76, 86, 86);

    artist.FillRectangle(Brushes.White, 76, 0, 86, 86);

    artist.Dispose();

    return canvas;

    }

    }

    In trying to adapt the same code (below) to display a QR code in an SSRS report I get the error "There is an error on line 1 of custom code: [BC30311] Value of type 'System.Drawing.Image' cannot be converted to '1-dimensional array of Byte.'

    Here is the custom code I am using.

    Public Function QRCode(ByVal RetailerId As String) as Byte()

    Return QRCode4CSCreateQRCode.CreateQRCodeClass.CreateQRCodeImage(RetailerId)

    End Function

    Here is the revised custom assembly.

    public class CreateQRCodeClass

    {

    public static byte[] CreateQRCodeImage(string inputString)

    {

    QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));

    qrcode.Make();

    Image canvas = new Bitmap(86, 86);

    Graphics artist = Graphics.FromImage(canvas);

    artist.Clear(Color.White);

    for (int row = 0; row < qrcode.GetModuleCount(); row++)

    {

    for (int col = 0; col < qrcode.GetModuleCount(); col++)

    {

    bool isDark = qrcode.IsDark(row, col);

    if (isDark == true)

    {

    artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    else

    {

    artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    }

    }

    artist.FillRectangle(Brushes.White, 0, 76, 86, 86);

    artist.FillRectangle(Brushes.White, 76, 0, 86, 86);

    artist.Dispose();

    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    canvas.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

    byte[] imagedata = null;

    imagedata = ms.GetBuffer();

    return imagedata;

    }

    }

    What data type can I successfully return to SSRS to display the image?

  • How about you just return data with the SQL Server datatype [var]binary(n)?

    This you can easily be displayed in Reporting Services reports.

    Would this work for you?

    Andreas

    ---------------------------------------------------
    MVP SQL Server
    Microsoft Certified Master SQL Server 2008
    Microsoft Certified Solutions Master Data Platform, SQL Server 2012
    www.insidesql.org/blogs/andreaswolter
    www.andreas-wolter.com

  • Andreas.Wolter (12/11/2013)


    How about you just return data with the SQL Server datatype [var]binary(n)?

    This you can easily be displayed in Reporting Services reports.

    Would this work for you?

    Thanks for the response.

    I am not sure. I have a bitmap in memory in PNG format in my assembly. Can that be converted to a [var]binary(n)?

  • I'll have to pass on that one

    I am not a .Net developer.

    All that SQL Server needs is the binary representation.

    But what exact code you need in order to do that, I have no idea

    Andreas

    ---------------------------------------------------
    MVP SQL Server
    Microsoft Certified Master SQL Server 2008
    Microsoft Certified Solutions Master Data Platform, SQL Server 2012
    www.insidesql.org/blogs/andreaswolter
    www.andreas-wolter.com

  • Stan for the portion where you stick the image into a table, you can do it via a CLR function;

    the CLR would look like this:

    [Microsoft.SqlServer.Server.SqlFunction()]

    public static SqlBytes CLR_GetFileImage(SqlString RetailerId)

    {

    try {

    return new SqlBytes(QRCode4CSCreateQRCode.CreateQRCodeClass.CreateQRCodeImage(RetailerId));

    } catch (SqlException sqlex) {

    throw new Exception("", sqlex);

    } catch (Exception ex) {

    throw new Exception("", ex);

    }

    }

    if QRCode4CSCreateQRCode is not a class that can be included in the project, but a compiled dll, you have to add it to your SQL server database, before you can add a reference to it for the CLR to use.

    i'm not sure on the real/full assembly name, but here's an example; you might have to enable CLR, and set your database to trustworthy before you can ever get it to deploy.

    --if the QRCode4CSCreateQRCode.CreateQRCodeClass assembly exists then drop it

    IF NOT EXISTS EXISTS (SELECT * FROM sys.assemblies WHERE [name] = 'QRCode4CSCreateQRCode.CreateQRCodeClass')

    BEGIN

    CREATE ASSEMBLY [QRCode4CSCreateQRCode.CreateQRCodeClass] AUTHORIZATION dbo

    FROM 'C:\Data\QRCode4CSCreateQRCode.dll'

    WITH PERMISSION_SET = UNSAFE

    END --IF EXISTS

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Andreas.Wolter (12/12/2013)


    I'll have to pass on that one

    I am not a .Net developer.

    All that SQL Server needs is the binary representation.

    But what exact code you need in order to do that, I have no idea

    You pointed me in the right direction anyway. Thanks!

  • That's just what I was looking for. Thanks!

  • Lowell (12/12/2013)

    ... you might have to enable CLR, and set your database to trustworthy before you can ever get it to deploy.

    ...

    [/code]

    Thanks for jumping in with a code-sample

    I would like to add though: Please do not set databases trustworthy without knowing the exact and complete security architecture.

    In 90% of all cases this leads to a setup which allows for privilege elevation to sysadmin.

    Use certificates instead if Unsafe Assemblies are necessary.

    Andreas

    ---------------------------------------------------
    MVP SQL Server
    Microsoft Certified Master SQL Server 2008
    Microsoft Certified Solutions Master Data Platform, SQL Server 2012
    www.insidesql.org/blogs/andreaswolter
    www.andreas-wolter.com

  • I figured out the answer.

    You have to convert the bitmap image to a byte array to display it in SSRS.

    This is what the revised code looks like that can be used with SSRS.

    public static Byte[] ReturnByteArray(string inputString)

    {

    QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));

    qrcode.Make();

    Image canvas = new Bitmap(86, 86);

    Graphics artist = Graphics.FromImage(canvas);

    artist.Clear(Color.White);

    for (int row = 0; row < qrcode.GetModuleCount(); row++)

    {

    for (int col = 0; col < qrcode.GetModuleCount(); col++)

    {

    bool isDark = qrcode.IsDark(row, col);

    if (isDark == true)

    {

    artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    else

    {

    artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    }

    }

    artist.FillRectangle(Brushes.White, 0, 76, 86, 86);

    artist.FillRectangle(Brushes.White, 76, 0, 86, 86);

    artist.Dispose();

    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    canvas.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

    byte[] imagedata = null;

    imagedata = ms.GetBuffer();

    return imagedata;

    }

  • Sounds like an interesting question, I am recently new to the barcode software[/url], feel headaches on how to use the qr code in ssrs reports. I think I gained something from this topic.

  • alex_2356 (6/26/2014)


    Sounds like an interesting question, I am recently new to the barcode software[/url], feel headaches on how to use the qr code in ssrs reports. I think I gained something from this topic.

    Here is an article I wrote a while back:

    Generate two-dimensional QR Code® bar codes in an SSRS report[/url]

  • Stan Kulp-439977 (12/14/2013)


    I figured out the answer.

    You have to convert the bitmap image to a byte array to display it in SSRS.

    This is what the revised code looks like that can be used with SSRS[/url].

    public static Byte[] ReturnByteArray(string inputString)

    {

    QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));

    qrcode.Make();

    Image canvas = new Bitmap(86, 86);

    Graphics artist = Graphics.FromImage(canvas);

    artist.Clear(Color.White);

    for (int row = 0; row < qrcode.GetModuleCount(); row++)

    {

    for (int col = 0; col < qrcode.GetModuleCount(); col++)

    {

    bool isDark = qrcode.IsDark(row, col);

    if (isDark == true)

    {

    artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    else

    {

    artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);

    }

    }

    }

    artist.FillRectangle(Brushes.White, 0, 76, 86, 86);

    artist.FillRectangle(Brushes.White, 76, 0, 86, 86);

    artist.Dispose();

    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    canvas.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

    byte[] imagedata = null;

    imagedata = ms.GetBuffer();

    return imagedata;

    }

    Thank you all posts!:-P

    I also meet a error in creating qr code in ssrs reports.

    So lucky found a resolution here.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply