如何在 SSRS 报表中动态调整图像的高度和宽度



我想根据表达式在 SSRS 报告中动态调整图像高度和宽度,但图像大小属性中没有可用的表达式选项,它只采用数值。我想调整图像大小,如下面的 c# 代码。

private void GetImageSize(string path)
{
    try
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(path);
        if (image != null)
        {
            System.Drawing.Image imageResized = ((System.Drawing.Image)image.Clone());
            int resizeWidth = 0;
            int resizeHeight = 0;
            bool heightIsLongerDimension = (imageResized.Height > imageResized.Width);
            float heightInches = (float)(imageResized.Height / imageResized.VerticalResolution);
            float widthInches = (float)(imageResized.Width / imageResized.HorizontalResolution);
            if (heightIsLongerDimension)
            {
                resizeHeight = (int)(imageResized.VerticalResolution * 3);
                //resizeWidth = Convert.ToInt32((((heightInches - 3) / heightInches) * widthInches) * imageResized.HorizontalResolution);
                resizeWidth = Convert.ToInt32((((float)imageResized.Width) / (float)imageResized.Height) * imageResized.HorizontalResolution) * 3;
            }
            else
            {
                resizeWidth = (int)(imageResized.HorizontalResolution * 3);
                //resizeHeight = Convert.ToInt32((((widthInches - 3) / widthInches) * heightInches) * imageResized.VerticalResolution);
                resizeHeight = Convert.ToInt32((((float)imageResized.Height) / (float)imageResized.Width) * imageResized.VerticalResolution) * 3;
            }
            //image height and width set in pixel
            Image1.Height = resizeHeight;
            Image1.Width = resizeWidth;
            //image height and width set in inches
            float width = (float)(Math.Round((resizeWidth / imageResized.HorizontalResolution), 1));
            float height = (float)(Math.Round((resizeHeight / imageResized.VerticalResolution), 1));
        }
    }
    catch
    {
        throw;
    }
}

在报表中使用自定义代码定义以下 vb.net 函数:

Public Function ResizeImage(ByVal picbytes as Byte()) As  Byte()
    Try
       Dim ms as System.IO.MemoryStream = Nothing
       Dim rms as System.IO.MemoryStream
       Dim bm as System.Drawing.Bitmap
       ms = new System.IO.MemoryStream(picbytes)
       bm = new System.Drawing.Bitmap(ms)
        Dim newWidth As Integer
        Dim newHeight As Integer
            Dim originalWidth As Integer =bm.Width
            Dim originalHeight As Integer = bm.Height
            Dim percentWidth As Single = CSng(1280) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(960) / CSng(originalHeight)
            Dim percent As Single = IIf(percentHeight < percentWidth, percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)

        Dim newImage As System.Drawing.Image = New System.Drawing.Bitmap(newWidth, newHeight)
        Using graphicsHandle As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(bm, 0, 0, newWidth, newHeight)
        End Using
        Dim ms2 = new  System.IO.MemoryStream()
        newImage.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg) 
        Dim bytes = ms2.ToArray()
        Return bytes
    Catch ex As Exception
        Return nothing
    End Try
End Function

在报表中引用 System.Drawing .NET 程序集。

然后你可以在表达式中重用函数,例如:

Code.ResizeImage(Fields!Bild.Value)

如果单击图像框并右键单击,您将看到属性选项。您可以单击图像,然后点击F4属性窗口将显示。那里你有大小选项。您将在那里看到各种选项。默认情况下,将自动选择"按比例拟合"。您可能希望更改它以适应大小。这将自动调整图像大小。此外,下面的链接将帮助您更详细地了解情况。看看吧。https://www.tutorialgateway.org/display-image-in-ssrs-report/

最新更新