Place text in image on edges

enter image description here This tool that I wrote in Visual Basic 2010 should add author text to images. The user can set the opacity and position of the font. To simplify the task, I need some presets for the position, which can be seen in the lower right corner. The calculation I use 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 great for this example. Where, as in some text, just clips. enter image description here

First: 1024x768 | Detected font size: 680x72

Second: 1688x1125 | Detected font size: 680x72

I suspect this has something to do with image proportions, but I don't know how to fix it.

The text is as follows:

  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.

+6
source share
2 answers

Does this problem only occur in your preview or in the converted file? Send the code to save the new image. I think you have sizemode installed in your image box, which is the problem. Try this without sizemode.

+1
source

It will be better to see more of your code, but as I understand it from the TextRenderer class, this is System.Windows.Forms. Just don't use Graphics created from a control (suppose it's a pictureBox with sizemode: Zoom), use Graphics created from your image.

Here is the code (sorry, C #) that loads the image from the file, draws the text, starting from the same coordinate and location on puctureBox1. Text always starts with a period (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 ); } } 
0
source

All Articles