簡體   English   中英

將水印圖像放在其他圖像上(C#,ASP.Net)

[英]Place watermark image on other images (C#, ASP.Net)

如何在其他圖像上添加水印圖像?

我能夠將圖像上的文字作為水印,但現在我有一個我想放在那里而不是文字的圖像。 我如何在C#中執行此操作?

再過一次具體,我有圖像X,我想用它作為水印符號。 我希望在我的網站上顯示時,此符號會顯示在我的所有圖像上。 因此,我將在圖像Y和Z上對圖像X進行水印處理。

這是我目前創建水印的代碼:

public static void AddWaterMark(MemoryStream ms, string watermarkText, MemoryStream outputStream)
        {
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            Graphics gr = Graphics.FromImage(img);
            Font font = new Font("Tahoma", (float)40);
            Color color = Color.FromArgb(50, 241, 235, 105);
            double tangent = (double)img.Height / (double)img.Width;
            double angle = Math.Atan(tangent) * (180 / Math.PI);
            double halfHypotenuse = Math.Sqrt((img.Height * img.Height) + (img.Width * img.Width)) / 2;
            double sin, cos, opp1, adj1, opp2, adj2;

            for (int i = 100; i > 0; i--)
            {
                font = new Font("Tahoma", i, FontStyle.Bold);
                SizeF sizef = gr.MeasureString(watermarkText, font, int.MaxValue);

                sin = Math.Sin(angle * (Math.PI / 180));
                cos = Math.Cos(angle * (Math.PI / 180));
                opp1 = sin * sizef.Width;
                adj1 = cos * sizef.Height;
                opp2 = sin * sizef.Height;
                adj2 = cos * sizef.Width;

                if (opp1 + adj1 < img.Height && opp2 + adj2 < img.Width)
                    break;
                //
            }

            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            gr.SmoothingMode = SmoothingMode.AntiAlias;
            gr.RotateTransform((float)angle);
            gr.DrawString(watermarkText, font, new SolidBrush(color), new Point((int)halfHypotenuse, 0), stringFormat);

            img.Save(outputStream, ImageFormat.Jpeg);
        }

在你調用gr.DrawString的同一個地方,你也可以做gr.DrawImage(position,size,overlayImage)。 如果從PNG文件(具有透明度)加載圖像到疊加層以產生最佳質量,則會有所幫助。

這是另一種使用GDI +的解決方案

使用GDI + for .NET創建水印照片

暫無
暫無

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

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