Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Magick.Net在用文本注释图像时应用背景色_C#_Imagemagick_Magick.net - Fatal编程技术网

C# 使用Magick.Net在用文本注释图像时应用背景色

C# 使用Magick.Net在用文本注释图像时应用背景色,c#,imagemagick,magick.net,C#,Imagemagick,Magick.net,我使用C和注释图像,如下所示: var text = "Variable text"; var img = new MagickImage("image.jpg"); img.FontPointsize = 50; img.FillColor = new MagickColor(Color.White); img.Annotate(text, Gravity.Northwest); var text = "Variable text"; var img = new MagickImage("i

我使用C和注释图像,如下所示:

var text = "Variable text";
var img = new MagickImage("image.jpg");
img.FontPointsize = 50;
img.FillColor = new MagickColor(Color.White);
img.Annotate(text, Gravity.Northwest);
var text = "Variable text";
var img = new MagickImage("image.jpg");

using (var imgText = new MagickImage())
{
     imgText.FontPointsize = 50;
     imgText.BackgroundColor = new MagickColor(Color.Black);
     imgText.FillColor = new MagickColor(Color.White);
     imgText.Read("label:" + text);
     img.Composite(text, Gravity.Northwest);
}
注释可以工作,但是文本并不总是容易阅读,因为它可以和图像混合

ImageMagick手册具有以下功能:

轮廓标签

画暗盒

只有在其他方法都不起作用的情况下,我才会使用此方法,因为它需要显式定义矩形的宽度和高度

底色盒

复合标签

自动大小字幕

花式标签

以上任何一种方法都可以。但是,我似乎找不到.NETAPI中公开的所需功能。例如,在调用Annotate之前,我尝试设置BackgroundColor。这没有产生任何效果:

img.BackgroundColor = new MagickColor(Color.Black);

我想了解一些关于如何使用Magick.Net实现增强注释文本可读性的方法的建议。

我最终实现了合成标签选项,如下所示:

var text = "Variable text";
var img = new MagickImage("image.jpg");
img.FontPointsize = 50;
img.FillColor = new MagickColor(Color.White);
img.Annotate(text, Gravity.Northwest);
var text = "Variable text";
var img = new MagickImage("image.jpg");

using (var imgText = new MagickImage())
{
     imgText.FontPointsize = 50;
     imgText.BackgroundColor = new MagickColor(Color.Black);
     imgText.FillColor = new MagickColor(Color.White);
     imgText.Read("label:" + text);
     img.Composite(text, Gravity.Northwest);
}
诀窍是“读取”图像,但要提供标签:用符号代替文件名。完成后,我们可以将生成的图像与原始图像合并


除了在文本注释中添加黑色背景外,此代码生成的结果与问题中发布的结果相同。

对于任何试图了解如何在图像上放置文本的人

这类似于我使用Magick.NET向图像添加文本的代码

将此代码用作LINQPad代码段。使用Magick.NET-Q16-AnyCPU nuget包

void Main()
{
    TextRender();
}

public void TextRender()
{
    //If you use a transparent color here, you won't see the text
    var imageBackgroundColor = new MagickColor("White");

    using (MagickImage image = new MagickImage(imageBackgroundColor, 400, 400))
    {
        var drawable = new DrawableText(0,10,"Line One\nLine Two\nLine Three");
        var gravity = new DrawableGravity(Gravity.North);
        var font = new DrawableFont("Arial");
        var antialias = new DrawableTextAntialias(true);
        var size = new DrawablePointSize(50);
        var color = new DrawableFillColor(Color.Black);
        //If needed
        //var strokeColor = new DrawableStrokeColor(Color.White);

        image.Annotate("Some annotation", Gravity.Center);

        image.Draw(drawable, gravity, font, antialias, size, color);//, strokeColor);

        var imageFileName = @"c:\temp\tempImage.jpg";

        //Write the file to disk
        image.Write(imageFileName);

        //For use in linqpad only
        //Load the written file from disk and show it in the Results window
        var image2 = (Bitmap) Image.FromFile(imageFileName, true);
        image2.Dump();
    }
}
结果是:


你也试过设置图像的笔划颜色吗?这是一个很好的提示,@dlemstra。StrokeColor在每个字母周围添加了一个轮廓,这提高了可读性。不过,对于我当前的应用程序,我更喜欢如下所示的合成标签。另外,感谢您创建Magick.Net。
convert -size 100x14 xc:none -gravity center \
  -stroke black -strokewidth 2 -annotate 0 'Faerie Dragon' \
  -background none -shadow 100x3+0+0 +repage \
  -stroke none -fill white     -annotate 0 'Faerie Dragon' \
  dragon.gif  +swap -gravity south -geometry +0-3 \
  -composite  anno_fancy.jpg
img.BackgroundColor = new MagickColor(Color.Black);
var text = "Variable text";
var img = new MagickImage("image.jpg");

using (var imgText = new MagickImage())
{
     imgText.FontPointsize = 50;
     imgText.BackgroundColor = new MagickColor(Color.Black);
     imgText.FillColor = new MagickColor(Color.White);
     imgText.Read("label:" + text);
     img.Composite(text, Gravity.Northwest);
}
void Main()
{
    TextRender();
}

public void TextRender()
{
    //If you use a transparent color here, you won't see the text
    var imageBackgroundColor = new MagickColor("White");

    using (MagickImage image = new MagickImage(imageBackgroundColor, 400, 400))
    {
        var drawable = new DrawableText(0,10,"Line One\nLine Two\nLine Three");
        var gravity = new DrawableGravity(Gravity.North);
        var font = new DrawableFont("Arial");
        var antialias = new DrawableTextAntialias(true);
        var size = new DrawablePointSize(50);
        var color = new DrawableFillColor(Color.Black);
        //If needed
        //var strokeColor = new DrawableStrokeColor(Color.White);

        image.Annotate("Some annotation", Gravity.Center);

        image.Draw(drawable, gravity, font, antialias, size, color);//, strokeColor);

        var imageFileName = @"c:\temp\tempImage.jpg";

        //Write the file to disk
        image.Write(imageFileName);

        //For use in linqpad only
        //Load the written file from disk and show it in the Results window
        var image2 = (Bitmap) Image.FromFile(imageFileName, true);
        image2.Dump();
    }
}