在C#中添加页脚到图像

时间:2015-05-29 16:20:59

标签: c# html image canvas stream

我正在使用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);

             }
         }

2 个答案:

答案 0 :(得分:2)

您可以创建新的位图,该位图将比原始图像更高,以适应下面的页脚。接下来将原始图像和页脚复制到该位图并保存新位图。

要做的方法是这样的(假设页脚宽度< =图像宽度):

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

答案 1 :(得分:1)

  

这是最简单的方法:我刚刚创建了新图像(页脚),一个新图像,上面绘制了旧图像和页脚图像。

                    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'是原始图片。

Here is the pic