Place Text in Image on Edges
Asked Answered
C

2

6

enter image description here This tool I wrote in Visual Basic 2010 should add an author text to images. The user is able to set the font opacity and position. To make things easier I wanted some position presets as one can see in the bottom right corner. The calculation I am using is (bottom right in this case:

 Dim textSize As Size = TextRenderer.MeasureText(tagString + curText, curFont)
 tmpPos = New Point(srcImg.Width - textSize.Width - 10, srcImg.Height - textSize.Height - 10)

As you can see this works perfectly for this example picture. Where as on some the text just clips out. enter image description here

First One: 1024x768 | Detected Font Size: 680x72

Second One: 1688x1125 | Detected Font Size: 680x72

I suspect this has something to do with the aspect ratio of the images but I do not know how to fix it.

The text is drawn like that:

 brush = New SolidBrush(color.FromArgb(alpha, color))
        gr = Graphics.FromImage(editImg)
        gr.DrawString(tagString + text, font, brush, pos)
        HauptBild.Image = editImg

I found this http://www.codeproject.com/Articles/20923/Mouse-Position-over-Image-in-a-PictureBox and it answered my questions.

Catboat answered 17/12, 2015 at 16:45 Comment(5)
can you include the code that places the text please?Multimillionaire
pls add some code, can you just put the code that modify the image?Doubledealing
Some codes will really be helpfulAyo
Sorry guys I have been busy the last few days. The sizemode is zoom because I want to fit any size of image into the imagebox. I will edit mit post and add some codeCatboat
Try setting the position based on the image container rather than image size, as image size will differ. i.e tmpPos = New Point(Picturebox1.Width - textSize.Width - 1, Picturebox1.Height - textSize.Height - 10)Active
D
1

is this problem only occuring in your preview or also in the converted File? Please post the Code how you save the New Image. I think you have Set a sizemode in your picturebox which is the Problem. Try it without the sizemode.

Deceptive answered 27/12, 2015 at 12:46 Comment(0)
H
0

Will be better to see more your code, but, as i understand by TextRenderer class it is System.Windows.Forms. Just do not use Graphics, created from control (i suppose it is pictureBox with sizemode:Zoom), use Graphics, created from your image instead.

Here is code (sorry, C#), which loads image from file, draws text starting from the same coordinate and places on puctureBox1. Text always starts from Point(100,100).

 OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.Filter = "Image files|*.jpeg;*.png;*.jpg;*.gif;*.bmp";
    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            Bitmap orig=(Bitmap)Bitmap.FromFile(openFileDialog1.FileName);
            //workaround for images with color table, see remarks here https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage(v=vs.110).aspx 
            Bitmap bmp=orig.Clone(new Rectangle(0, 0, orig.Width, orig.Height), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawString("hello", new Font(this.Font.FontFamily,30,FontStyle.Bold )  , new System.Drawing.SolidBrush(System.Drawing.Color.Yellow ), new Point(100, 100));
            this.pictureBox1.Image = bmp;
            orig.Dispose();

        }
        catch (Exception ex)
        {
            MessageBox.Show("Something goes wrong: " + ex.Message+ "\\n"+ ex.StackTrace );
        }
    }
Hillary answered 30/12, 2015 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.