RS 呼叫自訂的組件,產生背景圖

範例程式:CustomAssemblyGenerateBackgroundImage

聽課的朋友需要 Report 的報表自動帶某些文字的浮水印,所以我建立一個 .NET 自訂組件如下

簡單建立輸入文字後產生 Bitmap 圖的 byte array 函數

using System;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
namespace GenerateImages
{
    public class Class1
    {
        public static byte[] CreateBitmapImage(string sImageText)
        {
            Bitmap objBmpImage = new Bitmap(1, 1);

            int intWidth = 0;
            int intHeight = 0;

            Font objFont = new Font("Arial", 40, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
            Graphics objGraphics = Graphics.FromImage(objBmpImage);
            intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
            intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
            objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
            objGraphics = Graphics.FromImage(objBmpImage);
            objGraphics.Clear(Color.White);
            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
            objGraphics.Flush();

            //傳回 Bitmap 物件 RS 不能用,需要 byte array
            //objBmpImage.Save(@"C:\inetpub\images\test.bmp");
            //return (objBmpImage);
            MemoryStream ms = new MemoryStream();
            objBmpImage.Save(ms,ImageFormat.Bmp);
            Byte[] bt=ms.ToArray();
            return (bt);
        }

    }
}

透過 gacutil 註冊

image

並修改 在 C:\Program Files\Microsoft SQL Server\MSRS12.MSSQLSERVER\Reporting Services\ReportServer\rssrvpolicy.config 設定可信賴的 Assembly

<CodeGroup
               class="UnionCodeGroup"
               version="1"
               PermissionSetName="FullTrust"
               Name="GenerateImages.Class1"
               Description="產生 image">
                          <IMembershipCondition
                               class="UrlMembershipCondition"
                               version="1"
                               Url="C:\Program Files\Microsoft SQL Server\MSRS12.MSSQLSERVER\Reporting Services\ReportServer\bin\GenerateImages.dll" />

</CodeGroup>

RS 2014 似乎只能參照 NET Framework 2.0

image

報表背景呼叫組件

image

透過參數設定背景要產生的文字

image

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: