我怎样才能使一个PNG图像(如内存流)到.NET的ReportViewer报告表面能使、图像、表面、内存

2023-09-02 01:56:31 作者:丶随风__丶

我有一个,我保存到流,这样我可以在一个的ReportViewer表面上显示其动态创建的图像。

I have a dynamically created image that I am saving to a stream so that I can display it on a ReportViewer surface.

设置:

Windows客户端应用程序(而不是WebForms的) 报告的数据源是一个对象的数据源,用动态生成流作为属性(CustomImage) Report.EnableExternalImages = TRUE Image.Source =数据库 Image.MIMEType =图像/ PNG Image.Value = =领域!CustomImage.Value

这是行不通的,但没有报告任何错误,只是显示在报表上表面上的空图像图标。所有其他字段都显示正常。

This is not working, but is not reporting any errors, just showing an empty image icon on the report surface. All other fields are displaying correctly.

有没有人有这种情况的工作code样?

Does anyone have a working code sample of this scenario?

推荐答案

我在做才能有一个不断变化的标志报告。但我利用报表参数传递值类似的东西。我看不出有任何理由为什么这个一般的方法是行不通的,如果图像是数据的一部分。

I am doing something similar in order to have a changing logo on reports however I utilise report parameters to pass the value. I don't see any reason why this general method wouldn't work if the images were part of the data.

实质上图像被传递过来的两个字段。第一个字段是MIME类型值,第二个字段是一个Base64的连接codeD包含图像内容的字符串。

Essentially the images are passed over two fields. The first field is the MIME Type value and the second field is a Base64 encoded string containing the image content.

第1步:您的图像转换为Base64编码。 (我们code总是通过 ImageFormat.Png 这种方法使MIME类型容易)

Step 1: Convert your image to Base64 encoding. (Our code always passes ImageFormat.Png to this method to make the MIME Type easy)

private static string ConvertImageToBase64(Image image, ImageFormat format)
{
    byte[] imageArray;

    using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream())
    {
        image.Save(imageStream, format);
        imageArray = new byte[imageStream.Length];
        imageStream.Seek(0, System.IO.SeekOrigin.Begin);
        imageStream.Read(imageArray, 0, imageStream.Length);
    }

    return Convert.ToBase64String(imageArray);
}

第2步:通过图像和MIME类型报告

reportParams[0] = new ReportParameter("ReportLogo", base64Logo);
reportParams[1] = new ReportParameter("ReportLogoMimeType", "image/png");

_reportViewer.LocalReport.SetParameters(reportParams);

第3步:在报告中设定的图像上的下列属性(不带引号):

MIMETYPE:" =参数ReportLogoMimeType.Value " ! 值:" = System.Convert.FromBase64String(!参数ReportLogo.Value) " 更新:作为赫拉尔的下方说,图像源必须设置为数据库 MIMEType: "=Parameters!ReportLogoMimeType.Value" Value: "=System.Convert.FromBase64String(Parameters!ReportLogo.Value)" UPDATE: As Gerardo's says below, the Image Source must be set to 'Database'

陷阱年轻球员: 通常情况下,图像看起来可怕的,就像他们一直在缩小,即使你传递的,这似乎是正确的尺寸的图像。这是因为报告呈现为打印(300 dpi的),而不是在屏幕上(通常是72或92 dpi)的。解决方法是在图像发送约3倍太大,设置它在报告中正确的尺寸,改变了浆纱属性中的形象为 FitProportional

Trap for young players: Often the images will look horrible and like they've been scaled even though you're passing in an image which seems to be the "right size". This is because the reports are rendered for print (300 dpi) and not the screen (usually 72 or 92 dpi). The fix is to send in an image about 3 times too big, set it's correct size in the report and change the "Sizing" property on the image to "FitProportional".