簡體   English   中英

C#根據原始圖像改變水印圖像大小

[英]C# change watermark image size depending upon original image

我在上傳之前正在為我的圖片加水印。 我面臨的問題是如果圖像很小,水印看起來很大..我想根據原始圖像改變水印圖像大小..

例如:水印圖像應為原始圖像的30%。 我在c#中這樣做:

imageGraphics.FillRectangle(watermarkBrush,new Rectangle(new Point(x,y),new Size(watermarkImage.Width + 1,watermarkImage.Height)));

我該怎么辦才能先獲得圖像尺寸,然后相應地改變水印圖像尺寸?

好吧......那樣的話:

Bitmap yourImage = ...;
Bitmap yourWatermark = ...;

int newWaterWidth = (int)((float)yourImage.Width * .3);
int newWaterHeight = (int)((float)yourImage.Height* .3);


using(Bitmap resizedWaterm = new Bitmap(newWaterWidth, newWaterHeight))
using(Graphics g = Graphics.FromImage((Image)resizedWaterm))
{
  g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  g.DrawImage(yourWatermark, 0, 0, newWaterWidth , newWaterHeight );
}

int x = ..., y = ...;
using(Graphics g2 = Graphics.FromImage((Image)resizedWaterm))
{
  g2.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
}

(未經測試,您還需要在...點填寫值)

調整大小的代碼: 調整圖像大小而不會丟失任何質量

希望這可以幫助!

NReco.VideoConverter.FFMpegConverter ffMpeg = new NReco.VideoConverter.FFMpegConverter();

            string pathToVideoFile = "D:\\Projects\\Project\\Db_Script\\TAR_Dummy\\TAR_Dummy\\Videos\\"+postedFile.FileName;
            string imagePath = "D:\\Watermarks\\30.png";
            string Id = Guid.NewGuid().ToString();

            //Convert videos from one format to another
            ffMpeg.ConvertMedia(pathToVideoFile, "D:\\Watermarks\\"+Id+".flv", Format.flv);

            //Add Aatermark to videos
            ffMpeg.Invoke("-i "+pathToVideoFile+ " -i "+ imagePath + " -filter_complex \"overlay=10:10\" D:\\Watermarks\\Images\\" + Id+".mp4");

            //Get video thumbnail
            ffMpeg.GetVideoThumbnail(pathToVideoFile, "D:\\Watermarks\\Images\\" + Id.Substring(Id.Length-5)+".jpg");

暫無
暫無

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

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