ImageMagick在图像上创建圆圈计数

时间:2018-03-28 03:34:36

标签: c# imagemagick imagick imagemagick-convert

嘿,我有以下Magick.net代码:

List<string> lFiles = new List<string>();
bool isBlankImage = false;

lFiles.Add(@"C:\Users\David\Pictures\1.jpg");
lFiles.Add(@"C:\Users\David\Pictures\blank.png");
lFiles.Add(@"C:\Users\David\Pictures\blank.png");
lFiles.Add(@"C:\Users\David\Pictures\blank.png");
lFiles.Add(@"C:\Users\David\Pictures\blank.png");

IMagickImage roundImg = new MagickImage();
IMagickImage mask = new MagickImage();
IMagickImage shadow = new MagickImage();
IMagickImage result = new MagickImage();

foreach (string tempFBProfileImg in lFiles)
{
     roundImg = new MagickImage(tempFBProfileImg);

     if (Regex.IsMatch(tempFBProfileImg.ToLower(), @"\bblank.png\b"))
     {
         result.Extent(360, 100, Gravity.West);
         images.Add(result);
         isBlankImage = true;
         break;
     }
     else
     {
         mask = new MagickImage("xc:black", 100, 100);
         mask.Settings.FillColor = MagickColors.White;
         mask.Draw(new DrawableCircle(50, 50, 50, 90));
         mask.HasAlpha = false;

         roundImg.Resize(100, 100);
         roundImg.Composite(mask, CompositeOperator.CopyAlpha);
         roundImg.Draw(new DrawableStrokeColor(MagickColors.Black), 
                       new DrawableStrokeWidth(1),
                       new DrawableFillColor(MagickColors.None),
                       new DrawableCircle(50, 50, 50, 90));

         shadow = new MagickImage("xc:none", 100, 100);
         shadow.Settings.FillColor = MagickColors.Black;
         shadow.Draw(new DrawableCircle(50, 50, 50, 90));
         shadow.Blur(0, 5);

         roundImg.Composite(shadow, CompositeOperator.DstOver);

         images.Add(roundImg);
         images.First().BackgroundColor = MagickColors.None;

         result = images.SmushHorizontal(-35);
         result.Resize(100, 100);
         result.Write(@"C:\Users\David\Pictures\final2.png");
     }                                    
}

var imgText = new MagickImage(MagickColors.Blue, 0, 0);

imgText.Settings.FontPointsize = 24;
imgText.BackgroundColor = MagickColors.White;
imgText.Settings.FillColor = MagickColors.Black;
imgText.Settings.TextAntiAlias = true;
imgText.Settings.FontFamily = "Arial";
imgText.Read("label: 10+", 45, 25);
string caption = "label:10+";
//imgText = new MagickImage(caption, settings);
//imgText.Draw(new DrawableText(100, 20, "10+"));
imgText.Page = new MagickGeometry(300, 100, 0, 0);

result.Composite(imgText, CompositeOperator.Over);
result.Write(@"C:\Users\David\Pictures\final2.png");

这看起来像这样:

enter image description here

但我希望它看起来像这样:

enter image description here

我遇到的问题就是制作一个带有黑色轮廓和阴影的红色圆圈,里面有文字XX +。此外,我看起来高低不知道如何将它定位在我需要的地方,但我无法找到它。我认为MagickGeometry会是它,但事实证明它不是。

更新1

更改一些代码我现在得到这个:

enter image description here

更改了代码:

roundImg.Composite(mask, CompositeOperator.CopyAlpha);
roundImg.Draw(new DrawableStrokeColor(MagickColors.Black),
              new DrawableStrokeWidth(1),
              new DrawableFillColor(MagickColors.None),
              new DrawableCircle(50, 50, 50, 90));
imgText.Composite(roundImg, CompositeOperator.Over);
imgText.Settings.FontPointsize = 24;
imgText.BackgroundColor = MagickColors.Red;
imgText.Settings.FillColor = MagickColors.White;
imgText.Settings.TextAntiAlias = true;
imgText.Settings.FontFamily = "Arial";
imgText.Read("label: 10+", 45, 25);
//string caption = "label:This is a very long caption line";
//imgText = new MagickImage(caption, settings);
//imgText.Draw(new DrawableText(100, 20, "10+"));
imgText.Page = new MagickGeometry(360, 100, 0, 0);
result.Composite(imgText, CompositeOperator.Over);
result.Write(@"C:\Users\David\Pictures\final2.png");

更新2

好的,我的代码正确的圆圈中有数字:

MagickImage circleText = new MagickImage(MagickColors.Transparent, 29, 29);
MagickImage circle = new MagickImage(MagickColors.Transparent, 29, 29);

circleText.Settings.BorderColor = MagickColors.Black;              
circleText.Settings.FontPointsize = 16;
circleText.Settings.FontWeight = ImageMagick.FontWeight.Bold;
circleText.BackgroundColor = MagickColors.Transparent;
circleText.Settings.FillColor = MagickColors.White;
circleText.Settings.TextGravity = Gravity.Center;
circleText.Settings.TextAntiAlias = true;
circleText.Settings.FontFamily = "Arial";
circleText.Border(2);
circleText.Read("label: 10+", 29, 29);
circle.Draw(new DrawableStrokeColor(MagickColors.Black),
            new DrawableStrokeWidth(1),
            new DrawableFillColor(MagickColors.Red),
            new DrawableCircle(14, 14, 10, 1));
circleText.Composite(circle, CompositeOperator.DstOver);

result.Composite(circleText, CompositeOperator.Over);
result.Write(@"C:\Users\David\Pictures\final2.png");

产生图像:

enter image description here

但现在我似乎无法显示文字的边框,而我仍然坚持如何将圆圈移动到预定的位置?

更新3

我终于让它移动到需要它的地方使用这段代码:

MagickImage circleText = new MagickImage(MagickColors.Transparent, 29, 29);
MagickImage circle = new MagickImage(MagickColors.Transparent, 29, 29);
MagickImage _shadow = new MagickImage();

circleText.Settings.BorderColor = MagickColors.Black;
circleText.Settings.FontPointsize = 16;
circleText.Settings.FontWeight = ImageMagick.FontWeight.Bold;
circleText.BackgroundColor = MagickColors.Transparent;
circleText.Settings.FillColor = MagickColors.White;
circleText.Settings.TextGravity = Gravity.Center;
circleText.Settings.TextAntiAlias = true;
circleText.Settings.FontFamily = "Arial";
circleText.Border(2);
circleText.Read("label: 10+", 29, 29);
circle.Draw(new DrawableStrokeColor(MagickColors.Black),
                    new DrawableStrokeWidth(1),
                    new DrawableFillColor(MagickColors.Red),
                    new DrawableCircle(14, 14, 10, 1));

_shadow = new MagickImage("xc:none", 29, 29);
_shadow.Settings.FillColor = MagickColors.Black;
_shadow.Draw(new DrawableCircle(14, 14, 10, 1));
_shadow.Blur(0, 15);

circle.Composite(_shadow, CompositeOperator.DstOver);

circleText.Composite(circle, CompositeOperator.DstOver);

result.Composite(circleText, 68, 9, CompositeOperator.Over);
result.Write(@"C:\Users\David\Pictures\final2.png");

但是当我尝试将阴影添加到圆圈时,它看起来像这样:

enter image description here

当使用精确的圆圈x,y,w,h作为原始的红色圆圈时,不确定为什么阴影会成为正方形...

0 个答案:

没有答案