简体   繁体   English

如何将位图图像从自定义程序集返回到SSRS报告?

[英]How to 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. 我正在使用QRCode4CS类( http://qrcode4cs.codeplex.com/releases/view/74015 )生成QR码。

I can use the following code to successfully return a bitmap image to a picturebox in a Windows Form Application. 我可以使用以下代码将位图图像成功返回到Windows窗体应用程序中的图片框。

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.' 尝试改编相同的代码(如下)以在SSRS报告中显示QR代码时,出现错误“自定义代码的第1行出现错误:[BC30311]类型'System.Drawing.Image'的值不能为转换为“一维字节数组”。

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? 我可以成功返回SSRS哪种数据类型来显示图像?

I figured out the answer. 我想出了答案。

You have to convert the bitmap image to a byte array to display it in SSRS. 您必须将位图图像转换为字节数组才能在SSRS中显示。

This is what the revised code looks like that can be used with SSRS. 这就是修改后的代码可以与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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM