簡體   English   中英

在C#中添加頁腳到圖像

[英]Adding footer to image in C#

我正在使用GetUserMedia API來捕獲圖像並將其繪制到畫布上。 使用canvas的toDataURL()我得到“ImageUrl”。 該URL將作為png圖像保存到本地文件。 我所看到的是,在保存圖像之前,在圖像底部(而不是圖像上方)添加一些關於圖像的注釋,就像頁腳一樣。 我有以下代碼。 任何人都可以建議我在c#中如何做到這一點。

     imageByte= Convert.FromBase64String(ImageUrl);
     using (var streamBitmap = new MemoryStream(imageByte))
        {
            using (var img = Image.FromStream(streamBitmap))
            {
                  img.Save(localPath);

             }
         }

您可以創建新的位圖,它將從原始圖像更高,以適應下面的頁腳。 接下來將原始圖像和頁腳復制到該位圖並保存新位圖。

要做的方法是這樣的(假設頁腳寬度<=圖像寬度):

public Bitmap AppendImageFooter(System.Drawing.Image bmp, System.Drawing.Image footer)
{
    //Create new image that will be bigger then original image to make place for footer
    Bitmap newImage = new Bitmap(bmp.Height+footer.Height,bmp.Width);

    //Get graphics from new Image and copy original image and next footer below
    Graphics g = Graphics.FromImage(newImage);
    g.DrawImage(bmp, new Point(0, 0));
    g.DrawImage(footer, new Point(0, bmp.Height));
    g.Dispose();

    return newImage;
}

你可以在這個地方的代碼中加入它:

var footer = Image.FromFile("path_to_your_footer.png");      
imageByte= Convert.FromBase64String(ImageUrl);
         using (var streamBitmap = new MemoryStream(imageByte))
            {
                using (var img = Image.FromStream(streamBitmap))
                {
                      var imageWithFooter = AppendImageFooter(img, footer);
                      imageWithFooter.Save(localPath);

                 }
             }

編輯回復評論中的其他問題:

您可以在運行時構建頁腳。 下面的示例代碼,當然您可以以您喜歡的任何風格繪制您喜歡的任何內容:

public Bitmap AppendImageFooter(System.Drawing.Image bmp, string text)
{
    //Create new image that will be bigger then original image to make place for footer
    Bitmap newImage = new Bitmap(bmp.Height+200,bmp.Width);

    //Get graphics and copy image and below the footer
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(bmp, new Point(0, 0));
    g.FillRectangle(new SolidBrush(Color.Black), 0, bmp.Height, bmp.Width, 200);
    g.DrawString(text, new Font("Arial", 14), new SolidBrush(Color.White), 20, bmp.Height + 20);
    //Anything else you like, circles, rectangles, texts etc..
    g.Dispose();

    return newImage;
}

這是最簡單的方法:我剛剛創建了新圖像(頁腳),一個新的圖像,在其上繪制舊圖像+頁腳圖像。

                    int footerHeight = 30;
                    Bitmap bitmapImg = new Bitmap(img);// Original Image
                    Bitmap bitmapComment = new Bitmap(img.Width, footerHeight);// Footer
                    Bitmap bitmapNewImage = new Bitmap(img.Width, img.Height + footerHeight);//New Image
                    Graphics graphicImage = Graphics.FromImage(bitmapNewImage);
                    graphicImage.Clear(Color.White);
                    graphicImage.DrawImage(bitmapImg, new Point(0, 0));
                    graphicImage.DrawImage(bitmapComment, new Point(bitmapComment.Width, 0));
                    graphicImage.DrawString("Hi, This is Vivek !", new Font("Arial", 15), new SolidBrush(Color.Black), 0, bitmapImg.Height + footerHeight / 6);
                    bitmapNewImage.Save(yourImagePath);
                    bitmapImg.Dispose();
                    bitmapComment.Dispose();
                    bitmapNewImage.Dispose();

'img'是原始圖片。

這是照片

暫無
暫無

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

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