繁体   English   中英

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

[英]How to return a bitmap image from a custom assembly to an SSRS report?

我正在使用QRCode4CS类( http://qrcode4cs.codeplex.com/releases/view/74015 )生成QR码。

我可以使用以下代码将位图图像成功返回到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;
    }
}

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

这是我正在使用的自定义代码。

Public Function QRCode(ByVal RetailerId As String) as Byte()
     Return QRCode4CSCreateQRCode.CreateQRCodeClass.CreateQRCodeImage(RetailerId)
End Function

这是修改后的自定义程序集。

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;
    }
}

我可以成功返回SSRS哪种数据类型来显示图像?

我想出了答案。

您必须将位图图像转换为字节数组才能在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