Text Alignment problem in picturebox...

May 28 2010 4:24 AM
Hi,

I like to place a  transparent text on the image and also like to save the work. for that, I have the picture box in which i draw the rectangle (using graphics paint) and inside that rectangle I typed some name using label box and stored the work, have done

When I opened the saved Image the position of text I typed in the label box not in the position where I try to place (on screen). It's saving the text in some other position.

How to co-ordinate the resolution using c#.
 
  1. Image bac;
  2. Bitmap myBitmap;
  3. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  4. {
  5. if (mybitmap == null)
  6. {
  7. mybitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());
  8.  
  9. }
  10.  
  11. rect = new Rectangle(e.X, e.Y, 0, 0);
  12. this.Invalidate();
  13.  
  14. }
  15.  
  16.  
  17. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  18. {
  19.  
  20.  
  21. switch (e.Button)
  22. {
  23. case MouseButtons.Left:
  24. {
  25. rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
  26. this.Invalidate();
  27.  
  28. break;
  29. }
  30.  
  31. }
  32. }
  33. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  34. {
  35.  
  36. if (mybitmap == null)
  37. {
  38. return;
  39. }
  40. using (Pen pen = new Pen(Color.Red, 2))
  41. {
  42. using (Graphics g = Graphics.FromImage(mybitmap))
  43. {
  44. e.Graphics.DrawRectangle(pen, rect);
  45.  
  46. if (label1.TextAlign == ContentAlignment.TopLeft)
  47. {
  48. e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds);
  49. g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds);
  50. mybitmap.PixelFormat);
  51. }
  52. else if (label1.TextAlign == ContentAlignment.TopCenter)
  53. {
  54. SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
  55. float left = ((float)this.Width + label1.Left) / 2 - size.Width / 2;
  56. RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
  57. e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);
  58.  
  59. }
  60. else
  61. {
  62. SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
  63. float left = (float)label1.Width - size.Width + label1.Left;
  64. RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
  65. e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);
  66. }
  67.  
  68. }
  69.  
  70. }
  71.  
  72. }
  73.  
  74. private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  75. {
  76.  
  77.  
  78. if (mybitmap != null)
  79. {
  80. SaveFileDialog SaveFD1 = new SaveFileDialog();
  81. SaveFD1.FileName = "";
  82. SaveFD1.InitialDirectory = "C";
  83. SaveFD1.Title = "save file Name";
  84. SaveFD1.Filter = "JPG|*.jpg|Bmp|*.bmp";
  85. if (SaveFD1.ShowDialog() == DialogResult.OK)
  86. {
  87.  
  88. System.IO.Stream filename = (System.IO.FileStream)SaveFD1.OpenFile();
  89.  
  90. int r = SaveFD1.FileName.Length;
  91. for (int r1 = 0; r1<=r;)
  92. {
  93. if (SaveFD1.FileName[r1] != '.')
  94. r1++;
  95. else
  96. {
  97. r = r1;
  98. break;
  99. }
  100. }
  101.  
  102. if (SaveFD1.FileName[++r] == 'j')
  103. {
  104. using (Graphics g = Graphics.FromImage(bac))
  105. {
  106. g.DrawImage(mybitmap, 0, 0);
  107. }
  108. bac.Save(filename, ImageFormat.Jpeg);
  109. }
  110. else if (SaveFD1.FileName[r] == 'b')
  111. {
  112. using (Graphics g = Graphics.FromImage(bac))
  113. {
  114. g.DrawImage(mybitmap, 0, 0);
  115. }
  116. bac.Save(filename, ImageFormat.Jpeg);
  117. }
  118. else
  119. {
  120. using (Graphics g = Graphics.FromImage(bac))
  121. {
  122. g.DrawImage(mybitmap, 0, 0);
  123. }
  124. bac.Save(filename, ImageFormat.Png);
  125. }
  126.  
  127. filename.Close();
  128.  
  129. }
  130. }
  131. }
  132.  
  133. private void openToolStripMenuItem_Click(object sender, EventArgs e)
  134. {
  135.  
  136. OpenFD.FileName = "";
  137. OpenFD.Title = "open image";
  138. OpenFD.InitialDirectory = "C";
  139. OpenFD.Filter = "JPEG|*.jpg|Bmp|*.bmp|All Files|*.*.*";
  140. if (OpenFD.ShowDialog() == DialogResult.OK)
  141. {
  142. file = OpenFD.FileName;
  143.  
  144. bac = Image.FromFile(file);
  145. pictureBox1.Image = bac;
  146. pictureBox1.Invalidate();
  147.  
  148.  
  149. }
  150.  
  151. }


If not use the image (loaded from drive) and use only the bitmap on the picture box the rectangle and text alignment (with same font size and same position) are perfect. if i use the image the problem occurs

 
  1. //in save tool tip
  2. if (SaveFD1.FileName[++r] == 'j')
  3. {
  4. bac = pictureBox1.Image;
  5. //bac = mybitmap;//no problem if I use this
  6. using (Graphics g = Graphics.FromImage(bac))
  7. {
  8. g.DrawImage (mybitmap,0,0);
  9.  
  10. }
  11. bac.Save(filename, ImageFormat.Jpeg);


Please tell me where i am going wrong...

Answers (1)