0

i want to copy an image from one blob container to another one. I am using the system.drawing to process an image and watermark it. this is throwing an error. i am new to c# ,copied this code from internet. everything works except for the watermarking activity.

I'm getting the following error when the watermark function is called:

2020-06-03T17:28:31.225 [Error] run.csx(150,17): error CS1069: The type name 'Image' could not be found in the namespace 'System.Drawing'. This type has been forwarded to assembly 'System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. 2020-06-03T17:28:31.279 [Error] run.csx(150,36): error CS0103: The name 'Image' does not exist in the current context

the objective is that i am trying to create a function app to watermark an image and copy it into another blob storage

can you please let me know how best to incorporate 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);
             }
         }
     }



Rufus L
  • 36,127
  • 5
  • 30
  • 43
user3607004
  • 1
  • 1
  • 6
  • 1
    If you're using .Net Core, you don't have System.Drawing available, you need to use System.Drawing.Common. If you don't have this assembly among your Dependencies, you can get install its NuGet package. Open up your `NuGet Package Manager` and search for it. – Jimi Jun 03 '20 at 22:01

1 Answers1

0

If you use .net core Azure function, we need to use the package System.Drawing.Common. Regarding how to install the sdk, please refer to the document

For example the function.proj file

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

    <ItemGroup>
       <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
    </ItemGroup>
</Project>
Jim Xu
  • 21,610
  • 2
  • 19
  • 39