Multiple text caption per Image

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
pablobhz
Posts: 76
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Multiple text caption per Image

Post by pablobhz »

Hello Everyone.
I was using Drawables to draw some text into an image - however, i'll need word wrap and based on what i've read, i can do that using captions.

However, i can't seem to figure out how to specify an (x,y) offset for the text - this way i could have multiple captions per image and draw all what i need.

For now, my code is:

Code: Select all

 var notaAgain = new MagickImage();
            var h1Settings = new MagickReadSettings()
            {                                
                BackgroundColor = MagickColors.White,
                Font = "PragatiNarrow-Bold",
                FontPointsize = 18,                
                FontWeight = FontWeight.Bold,
                Width = DanfeSettings.Width,
                Height = DanfeSettings.Height            
            };
            notaAgain.Read("caption: eNotas", h1Settings,);
            notaAgain.Write(@"C:\test\naosei.png");
On the line

Code: Select all

notagain.Read
i'm writing my text. What i can't seem to figure out is a way to write multiple text (something like, notaAgain.Read multiple times specifying an offset for the caption).

Is it possible ?

Thanks !

snibgo
Posts: 12899
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple text caption per Image

Post by snibgo »

"caption:" gives you a single image. You can do this multiple times to get multiple images. You can then combine these into a single image by appending or compositing them.
snibgo's IM pages: im.snibgo.com

pablobhz
Posts: 76
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Multiple text caption per Image

Post by pablobhz »

snibgo wrote:
2019-11-13T09:40:56-07:00
"caption:" gives you a single image. You can do this multiple times to get multiple images. You can then combine these into a single image by appending or compositing them.
I know about Composing and Combine (MagickCollection.Combine). How would i combine those multiple images ?

I know i can't just create an array and append to the end.

User avatar
fmw42
Posts: 26262
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiple text caption per Image

Post by fmw42 »

I do not know if this is relevant to your issue. But you can insert new line break "\n" into your text, if you want to put spacing between text in label: or make paragraphs in caption:

pablobhz
Posts: 76
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Multiple text caption per Image

Post by pablobhz »

fmw42 wrote:
2019-11-13T10:20:05-07:00
I do not know if this is relevant to your issue. But you can insert new line break "\n" into your text, if you want to put spacing between text in label: or make paragraphs in caption:
Its relevant and i've found out it a few minutes ago - Thanks !
HOwever i'm drawing an coupon receipt ; can't use newline always.

Lets say that the first block is the coupon description (company name, address, etc), and the second block is going to be item description.

I'm doing it because when drawing the image using GDI+ (Bitmap + Graphics) my results when printing were horrible. Only when using the PrintPageEventArgs Graphics i got nicer results ; however, this doesn't allow me to save (only to draw). Thats why i've decided go with MagickNET for this task.

Right now what i'm looking for is an way to append an image to the end of another
ex: You have images A B C D
what i'm trying to do is:
A
B
C
D

as an full image.

Thanks !

snibgo
Posts: 12899
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple text caption per Image

Post by snibgo »

I don't use Magck.NET. I suggest you search this forum for examples, such as https://www.imagemagick.org/discourse-s ... nd#p158866
snibgo's IM pages: im.snibgo.com

pablobhz
Posts: 76
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Multiple text caption per Image

Post by pablobhz »

snibgo wrote:
2019-11-13T11:24:44-07:00
I don't use Magck.NET. I suggest you search this forum for examples, such as https://www.imagemagick.org/discourse-s ... nd#p158866
Thanks ! But i've figured out my solution.
Here's my code, to help others later:

Code: Select all

var finalCollection = new MagickImageCollection();
            var nFantasia = new MagickImage();
            var razaoSocial = new MagickImage();
            var cnpjAndIe = new MagickImage();
            var logradouroComNumero = new MagickImage();
            var bairro = new MagickImage();
            var cidadeComCEP = new MagickImage();
            
            var h1FontNoLineBreak = new MagickReadSettings()
            {

                BackgroundColor = MagickColors.White,
                Font = "Tahoma",
                FontPointsize = 18,
                FontWeight = FontWeight.Bold,
                FontStyle = FontStyleType.Bold,
                Width = DanfeSettings.Width,
                Height = 18,
                TextGravity = Gravity.Center
            };
            var h2FontWithLineBreak = new MagickReadSettings()
            {

                BackgroundColor = MagickColors.White,
                Font = "Tahoma",
                FontPointsize = 14,
                FontWeight = FontWeight.Bold,                
                Width = DanfeSettings.Width,
                Height = 28,
                TextGravity = Gravity.Center
            };
            var h3FontNoLineBreak = new MagickReadSettings()
            {

                BackgroundColor = MagickColors.White,
                Font = "Tahoma",
                FontPointsize = 12,
                FontWeight = FontWeight.Bold,
                Width = DanfeSettings.Width,
                Height = 12                
            };
            
            nFantasia.Read("caption:" + empresa._nomeFantasia, h1FontNoLineBreak);
            razaoSocial.Read("caption:" + empresa._razaoSocial, h2FontWithLineBreak);
            cnpjAndIe.Read("caption:" +  empresa._cnpj + "    " + empresa._inscricaoEstadual, h3FontNoLineBreak);
            logradouroComNumero.Read("caption:" + nfce._cliente._endereco._logradouro + " " + nfce._cliente._endereco._numero, h3FontNoLineBreak);
            bairro.Read("caption:" + nfce._cliente._endereco._bairro, h3FontNoLineBreak);
            cidadeComCEP.Read("caption:" + nfce._cliente._endereco._nomeMunicipio + "/" + nfce._cliente._endereco._UF + " - " + nfce._cliente._endereco._CEP, h3FontNoLineBreak);
            
            MagickReadSettings finalCompositeSettings = new MagickReadSettings();
            finalCompositeSettings.Width = DanfeSettings.Width;
            finalCompositeSettings.Height = nFantasia.Height + razaoSocial.Height + cnpjAndIe.Height + logradouroComNumero.Height + bairro.Height + cidadeComCEP.Height;

            MagickImage finalImg = new MagickImage("xc:white", finalCompositeSettings);
            float compositeHeight = 0;
            finalImg.Composite(nFantasia, new PointD(0, 0));
            compositeHeight += nFantasia.Height;
            finalImg.Composite(razaoSocial, new PointD(0, compositeHeight));
            compositeHeight += razaoSocial.Height;
            finalImg.Composite(cnpjAndIe, new PointD(0, compositeHeight));
            compositeHeight += logradouroComNumero.Height;
            finalImg.Composite(logradouroComNumero, new PointD(0, compositeHeight));
            compositeHeight += bairro.Height;
            finalImg.Composite(bairro, new PointD(0, compositeHeight));
            compositeHeight += cidadeComCEP.Height;
            finalImg.Composite(cidadeComCEP, new PointD(0, compositeHeight));
            finalImg.Write(@"C:\test\magickteste_02.png");
            var gdiBmp = finalImg.ToBitmap();
            pictureBox1.Image = (Image)gdiBmp;
            
Since its a test thing only, i'm not worrying about disposing anything etc...in the end the test didn't had the results i wanted hehe.

Thanks !

Post Reply