【问题标题】:How to rotate an image in SSRS如何在 SSRS 中旋转图像
【发布时间】:2014-06-15 17:00:51
【问题描述】:

我正在使用 SQL 在 SSRS 中构建报告,其中我使用下面的代码将条形码转换为图像,此代码取自 here。我不是VB.Net 开发人员,但这段代码非常适合我。 问题是我的报告的布局是垂直条形码图像而不是水平的,我看不到任何可以旋转图像 OOB 的选项。任何人都可以在这里帮助我使用以下代码旋转图像。

Public Shared Function GenerateImage(ByVal fontName As String, ByVal stringText As String) As Byte()

    Dim oGraphics As System.Drawing.Graphics
    Dim barcodeSize As System.Drawing.SizeF
    Dim ms As System.IO.MemoryStream
    Dim i As System.Drawing.Image

    Using font As New System.Drawing.Font(New System.Drawing.FontFamily(fontName), 36)
    Using tmpBitmap As New System.Drawing.Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    oGraphics = System.Drawing.Graphics.FromImage(tmpBitmap)
    oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel

    barcodeSize = oGraphics.MeasureString(stringText, font)
    oGraphics.Dispose()
    End Using

    Using newBitmap As New System.Drawing.Bitmap(barcodeSize.Width, barcodeSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    oGraphics = System.Drawing.Graphics.FromImage(newBitmap)
    oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel


    Using oSolidBrushWhite As New System.Drawing.SolidBrush(System.Drawing.Color.White)
    Using oSolidBrushBlack As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
    oGraphics.FillRectangle(oSolidBrushWhite, New System.Drawing.Rectangle(0, 0, barcodeSize.Width, barcodeSize.Height))
    oGraphics.DrawString(stringText, font, oSolidBrushBlack, 0, 0)

    End Using

    End Using

    ms = New System.IO.MemoryStream()
    newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    End Using
        End Using

    Return ms.ToArray()
    End Function

我绝对是 VB.Net 中的 NOOB。

【问题讨论】:

  • 你可能只需要bmp.RotateFlip(RotateFlipType.Rotate270FlipNone) 或类似的,这取决于它被翻转的方向。
  • 对示例项目的更改是在 newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png) 之前添加 newBitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone) 并将报告中的 ImageProperties 设置为“原始大小”而不是“剪辑”顺便说一句。
  • 添加了一些其他观察/提示作为答案
  • @hkhan 根据我之前的评论使用完全限定名称引用它。您也许可以添加imports System.Drawing,但我看到您的示例代码的其余部分没有这样做,所以这在 RS 嵌入式代码中可能是不可能的。

标签: css vb.net reporting-services


【解决方案1】:

Bitmap 类有几个内置的旋转和翻转机制:

Dim bmp As Bitmap = <bitmap from somewhere>

bmp.RotateFlip(RotateFlipType.Rotate270FlipNone)

这会将图像顺时针旋转 270 度,与逆时针旋转 90 度相同。存在许多其他轮换选项,例如:

Rotate270FlipXY
Rotate270FlipY
Rotate270FlipX

以及基于Rotate90...Rotate180... 的变体


我还应该指出,您可以将它们组合起来,而不是嵌套 Using 语句:

Using font As New Font(New FontFamily(fontName), 36),
       tmpBitmap As New Bitmap(1, 1, Imaging.PixelFormat.Format32bppArgb)
       Br As New Brush(.....),
       otherBMP As New Bitmap(....)

       <your code here>

                       <rather than here>

这减少了许多人认为烦人的代码缩进量。您还可以通过在顶部添加 Imports 语句(C# 中的 using)来缩短其中的一些引用,例如 System.Drawing.Font

Imports System.Drawing

(显然您目前不能将Imports 与 SSRS 一起用作评论中的链接,因此请忽略该部分)。

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 2017-04-14
  • 2016-02-04
  • 2015-04-04
  • 2011-06-25
  • 2016-07-20
  • 2021-11-30
相关资源
最近更新 更多