簡體   English   中英

水印圖像無效,獲取空白圖像

[英]Watermark image doesn't work, get Blank image

我正在嘗試在ASP.NET中的圖像上應用水印文本。 我想使用圖像流而不是使用/寫入本地文件。

這是代碼:

 using (MemoryStream ms_small = new MemoryStream())
  {

       System.Drawing.Image image_small = System.Drawing.Image.FromStream(filestream); 
       width = image_small.Width;
       height = image_small.Height;


       string Copyright = "Hi I am copyright watermark";

       Graphics grPhoto = Graphics.FromImage(image_small);

       int phWidth = image_small.Width;
       int phHeight = image_small.Height;

        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

       int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
       Font crFont = null;
       SizeF crSize = new SizeF();


       for (int i = 0; i < 7; i++)
       {
         crFont = new Font("arial", sizes[i], FontStyle.Bold);
         crSize = grPhoto.MeasureString(Copyright, crFont);
         if ((ushort)crSize.Width < (ushort)phWidth)
            break;
       }

       int yPixlesFromBottom = (int)(phHeight * .05);

       float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

       float xCenterOfImg = (phWidth / 2);


       StringFormat StrFormat = new StringFormat();
       StrFormat.Alignment = StringAlignment.Center;


       SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));


        grPhoto.DrawString(Copyright,             
               crFont,                                  
               semiTransBrush2,                         
               new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
                            StrFormat);

            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
            grPhoto.DrawString(Copyright,            
                 crFont,                           
                 semiTransBrush,                          
                 new PointF(xCenterOfImg, yPosFromBottom), 
                 StrFormat);                      

                   Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);
                   image_small = (Image)outputBitMap;
                   ImageBuilder.Current.Build(image_small, ms_small, rs_small_jpg);

               }

水印代碼是從這里開始的

同樣,我要實現的是在圖像頂部添加水印文本,而無需在本地寫入圖像或使用臨時的本地圖像文件來應用水印。 我得到的是空白圖像,而不是帶有水印的圖像。

您的問題是在方法末尾使用了用於圖像的圖形。 相反,您必須使用原始圖像,因為所有圖形均已繪制在其中。

替換此字符串:

Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);

有了這個:

Bitmap outputBitMap = new Bitmap(imageSmall);

祝好運!

您的問題不清楚,如果您需要將生成的圖像顯示在asp.net的頁面中, HttpHandler是您想要的東西。 這篇文章是希望對您有所幫助的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM