简体   繁体   English

在C#中添加页脚到图像

[英]Adding footer to image in C#

I am using GetUserMedia API to capture image and draw it to canvas. 我正在使用GetUserMedia API来捕获图像并将其绘制到画布上。 Using toDataURL() of canvas I get the "ImageUrl". 使用canvas的toDataURL()我得到“ImageUrl”。 The url is saved as png image to local file. 该URL将作为png图像保存到本地文件。 What I am looking is that before saving the image add some comment about image at bottom of image(not over the image) like a footer. 我所看到的是,在保存图像之前,在图像底部(而不是图像上方)添加一些关于图像的注释,就像页脚一样。 I have the below code. 我有以下代码。 Can any one suggest me how to do this in c#. 任何人都可以建议我在c#中如何做到这一点。

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

             }
         }

You can create new Bitmap which will be higher from the original image to fit also the footer below. 您可以创建新的位图,它将从原始图像更高,以适应下面的页脚。 Next copy the original image and footer to that bitmap and save the new bitmap. 接下来将原始图像和页脚复制到该位图并保存新位图。

The method to do would like this (assuming the footer width <= image width): 要做的方法是这样的(假设页脚宽度<=图像宽度):

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;
}

And you can fit it in your code at this place: 你可以在这个地方的代码中加入它:

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);

                 }
             }

Edited in reply to additional question from comments: 编辑回复评论中的其他问题:

You can build the footer in runtime. 您可以在运行时构建页脚。 Sample code below, of course you can draw whatever you like in whatever style you like: 下面的示例代码,当然您可以以您喜欢的任何风格绘制您喜欢的任何内容:

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;
}

Here is the simplest way: I just created new image(footer), a new Image on which old image + footer image is drawn. 这是最简单的方法:我刚刚创建了新图像(页脚),一个新的图像,在其上绘制旧图像+页脚图像。

                    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' is the original Image. 'img'是原始图片。

这是照片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM