0

我想将图像从一个 blob 容器复制到另一个容器。我正在使用 system.drawing 处理图像并为其添加水印。这是抛出一个错误。我是 c# 的新手,从互联网上复制了这段代码。除了水印活动外,一切正常。

调用水印函数时出现以下错误:

2020-06-03T17:28:31.225 [错误] run.csx(150,17):错误 CS1069:在命名空间“System.Drawing”中找不到类型名称“Image”。此类型已转发到程序集 'System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 考虑添加对该程序集的引用。2020-06-03T17:28:31.279 [错误] run.csx(150,36):错误 CS0103:当前上下文中不存在名称“图像”

目标是我正在尝试创建一个函数应用程序来为图像添加水印并将其复制到另一个 blob 存储中

你能告诉我如何最好地合并 System.Drawing.Common

// i want to copy an image from one blob storage to another and watermark it . for it i am using System.Drawing name space instead it asks me to use System.Drawing.Common'. I am new to c# just copied the code from a previously existing work. 

#r "Newtonsoft.Json"
#r "System.Drawing"

using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using Microsoft.Extensions.Logging;


public static void Run(Stream myBlob, string name, Stream outputBlob, ILogger log)
{
    log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    bool result = IsGoodByModerator(myBlob, name, log);
    log.LogInformation("Image Moderation " + (result ? "Passed" : "Failed"));
    try{
        string watermarkText = "PwC";
        WriteWatermark(watermarkText,myBlob,outputBlob);
        log.LogInformation("Added watermark and copied successfully");
    }
    catch(Exception ex)
    {
        log.LogInformation("Watermark Error: " + ex.ToString());
    }    
}

//Add following method to add text as watermark on the image

    private static void WriteWatermark(string watermarkContent, Stream originalImage, Stream newImage)
     {
         originalImage.Position = 0;
         **using (Image inputImage = Image**// i get an error here**strong text**
           .FromStream(originalImage, true))
         {
             using (Graphics graphic = Graphics
              .FromImage(inputImage))
             {
                 Font font = new Font("Georgia", 36, FontStyle.Bold);
                 SizeF textSize = graphic.MeasureString(watermarkContent, font);

                 float xCenterOfImg = (inputImage.Width / 2);
                 float yPosFromBottom = (int)(inputImage.Height * 0.90) - (textSize.Height / 2);

                 graphic.SmoothingMode = SmoothingMode.HighQuality;
                 graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                 graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                 graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;


                 StringFormat StrFormat = new StringFormat();
                 StrFormat.Alignment = StringAlignment.Center;

                 SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
                 graphic.DrawString(watermarkContent, font, semiTransBrush2, xCenterOfImg + 1, yPosFromBottom + 1, StrFormat);

                 SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
                 graphic.DrawString(watermarkContent, font, semiTransBrush, xCenterOfImg, yPosFromBottom, StrFormat);

                 graphic.Flush();
                 inputImage.Save(newImage, ImageFormat.Jpeg);
             }
         }
     }



4

1 回答 1

0

如果你使用 .net core Azure 功能,我们需要使用 package System.Drawing.Common。关于如何安装sdk,请参考文档

例如 function.proj 文件

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
       <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
    </ItemGroup>
</Project>
于 2020-06-04T03:16:39.980 回答