 |
|
 |
I tried to add more objects on the window, such as multiple text objects, rectangles.
I want to move them separately. But they were moving together when I pressed the mouse.
How can I move/operate multiple objects independently.
|
|
|
|
 |
|
|
 |
|
 |
Hi Sir,
With lots of twists and turns I am planning to improve this code in a better way and I need your help in this.
I have two forms and on the first form I have 2 buttons and a picturebox (Button1).For Opening an image and (Button2).Edit the image.
If I click edit you form should open and I can edit the text by rotating the text at a certain angle and after closing this form I am showing the watermarked image on the first form picturebox.
And If I click again edit button I need to show the rotated text with its position which I am unable to get it.
So how do I save this setting and open with the same when I click the edit button.
|
|
|
|
 |
|
 |
It depends on how you are doing this.
In general however, you have to keep the Image and the Text objects separate until you are done and want to save the compiled image. When you close the edit window make sure you are not erasing the information for the text object, so when you re open it, it can rebuild the complete image.
You got me intriged on this subject again and I fell into a little free time to waste, so I have been looking at this project again. I hope to write an article on it soon. I think it will be more of what you are looking for. On screen editing, multiple text objects save as image or save as a object file so it can be re-opened and re-edited, plus a bunch of other stuff. I just need to tweak it a bit more and then I will publish it.
SSDiver2112
|
|
|
|
 |
|
 |
Thank you sir and I will be waiting for the next article.And once again sorry for disturbing you.
|
|
|
|
 |
|
|
 |
|
 |
Hi Sir,
I would like to paint the same thing on the picture box instead on the graphics path.So that we can reduce the size while loading the image.Is it possible to do so?
And I tried the same thing but when I tried to paint it on the picturebox I am not able to rotate or move the text.
|
|
|
|
 |
|
 |
Hi Sir,
I am loading the image using open file dialog but when loading the image if it is more pixels it is covering the whole form and mouse over text event is not responding much faster instead of that I need to resize the image while loading and display it.
Can you say me how to do that?
|
|
|
|
 |
|
 |
The question is do you really want to re-size the image? If you shrink the image and then add the text you will end up with a small image.
If it is do this:
Using ofdlg As New Windows.Forms.OpenFileDialog
If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim img As Drawing.Image = Drawing.Image.FromFile(ofdlg.FileName)
Dim btmap As New Drawing.Bitmap(img, 200, 200)
End If
End Using
If you don't really want to re-size it. Then you will have to work with zooming and/or panning the image.
That isn't and easy example, and I am still trying to perfect this myself. Maybe look around in CodeProject for an example of this.
SSDiver2112
|
|
|
|
 |
|
 |
Thank you sir and I am looking forward for more articles like this from you.And just give me a buzz once you finish that project.
|
|
|
|
 |
|
 |
Hi Sir,
I have a small and simple question as I am new to GDI and I would like to that when I am changing the font using Font dialog it changes the size but when I give the last size (ie..72) there comes the problem as the full text on the images is not showing and if I change the code here
Dim emsize As Single = Me.CreateGraphics.DpiY * pic_font.SizeInPoints / 74
Instead of 74 If I give higher number the text is not rotating in its own axis.
Can you please let me know how to change this?
modified on Tuesday, June 21, 2011 6:47 AM
|
|
|
|
 |
|
 |
After changing the font it needs to be reset. Try this:
If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
pic_font = FontDialog1.Font
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
Me.Invalidate()
End If
If you just want to change the Font Size you can do it this way:
Private Sub cboFontSize_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cboFontSize.SelectedIndexChanged
pic_font = New Font("Arial Black", CSng(cboFontSize.Text), _
FontStyle.Regular, GraphicsUnit.Pixel)
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
Me.Invalidate()
End Sub
Hope this helps,
SSDiver2112
|
|
|
|
 |
|
 |
Thank you for your quick response and it helps me a lot and works perfectly.
|
|
|
|
 |
|
 |
As a someday article I have been working on and off, on a better version that allows you to drop multiple text objects on the picture and rotate and size each independently with the mouse. I just have to find the time to finish it.
For now though replace the form code in this article with the code below. Add the font size Combobox or the Font Dialog as you see fit and tweak that part of the code. This will let you use the mouse to rotate the text directly.
Imports System.Drawing.Drawing2D
Public Class Form1
Dim rAngle As Integer
Dim sAngle As Integer
Dim pic_font As Font
Dim bm As Bitmap
Dim tbm As Bitmap
Dim strText As String = "Diver Dude"
Dim szText As New SizeF
Dim ptText As New Point(125, 125)
Dim ptsAngle() As PointF
Dim ptOrigin As PointF
Dim ptsText() As PointF
Dim ptsRotateText() As PointF
Dim ptsTextPen As Pen = New Pen(Color.LightSteelBlue, 1)
Dim MovingOffset As PointF
Dim MouseMoving As Boolean
Dim MouseRotating As Boolean
Dim MouseOver As Boolean
Public Sub New()
MyBase.New()
InitializeComponent()
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ptsTextPen.DashStyle = DashStyle.Dot
bm = My.Resources.DivePic
Dim FSize() As Single = {4, 6, 8, 10, 12, 13, 14, 15, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 46, 50, 56, 60, 72, 80}
Dim FS As Single
For Each FS In FSize
cboFontSize.Items.Add(FS)
Next
cboFontSize.SelectedIndex = cboFontSize.FindString("40")
pic_font = New Font("Arial Black", CSng(cboFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If IsMouseOverRotate(e.X - 10, e.Y - 10) Then
MouseRotating = True
ptOrigin = New PointF(ptText.X + (szText.Width / 2), ptText.Y + (szText.Height / 2))
sAngle = getAngle(ptOrigin, e.Location) - rAngle
ElseIf IsMouseOverText(e.X - 10, e.Y - 10) Then
MouseMoving = True
MovingOffset.X = e.X - ptText.X
MovingOffset.Y = e.Y - ptText.Y
Else
MouseMoving = False
MouseRotating = False
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
If MouseMoving Then
ptText.X = CInt(e.X - MovingOffset.X)
ptText.Y = CInt(e.Y - MovingOffset.Y)
Me.Invalidate()
ElseIf MouseRotating Then
rAngle = getAngle(ptOrigin, e.Location) - sAngle
Me.Invalidate()
lblRotate.Text = getAngle(ptOrigin, ptsAngle(0))
lblRotate.Refresh()
End If
Else
If IsMouseOverRotate(e.X - 10, e.Y - 10) Then
Me.Cursor = Cursors.Hand
If Not MouseOver Then
MouseOver = True
Me.Invalidate()
End If
ElseIf IsMouseOverText(e.X - 10, e.Y - 10) Then
Me.Cursor = Cursors.SizeAll
If Not MouseOver Then
MouseOver = True
Me.Invalidate()
End If
Else
Me.Cursor = Cursors.Default
If MouseOver Then
MouseOver = False
Me.Invalidate()
End If
End If
End If
End Sub
Private Function getAngle(ByVal Origin As PointF, ByVal XYPoint As PointF) As Integer
Dim xLength As Single = XYPoint.X - Origin.X
Dim yLength As Single = XYPoint.Y - Origin.Y
Dim TheAngle As Single
If xLength = 0 And yLength = 0 Then Return 0
If xLength = 0 And yLength < 0 Then Return 0
If yLength = 0 And xLength > 0 Then Return 90
If xLength = 0 And yLength > 0 Then Return 180
If yLength = 0 And xLength < 0 Then Return 270
TheAngle = Math.Atan(xLength / yLength)
TheAngle = TheAngle * (180 / Math.PI)
If yLength > 0 Then
TheAngle = 180 - TheAngle
ElseIf xLength > 0 Then
TheAngle = Math.Abs(TheAngle)
ElseIf xLength < 0 Then
TheAngle = 360 - TheAngle
End If
Return CInt(TheAngle)
End Function
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
MouseMoving = False
MouseRotating = False
Me.Invalidate()
End Sub
Public Function IsMouseOverText(ByVal X As Integer, ByVal Y As Integer) As Boolean
Using gp As New GraphicsPath()
gp.AddPolygon(ptsText)
Return gp.IsVisible(X, Y)
End Using
End Function
Public Function IsMouseOverRotate(ByVal X As Integer, ByVal Y As Integer) As Boolean
Using gp As New GraphicsPath()
gp.AddPolygon(ptsRotateText)
Return gp.IsVisible(X, Y)
End Using
End Function
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
tbm = CType(bm.Clone, Bitmap)
Dim g As Graphics = Graphics.FromImage(tbm)
Dim mx As Matrix = New Matrix
Dim gpathText As New GraphicsPath
Dim br As SolidBrush = New SolidBrush(Color.FromArgb(tbarTrans.Value, _
Color.LightCoral))
SetptsText()
g.SmoothingMode = SmoothingMode.AntiAlias
Dim emsize As Single = Me.CreateGraphics.DpiY * pic_font.SizeInPoints / 72
gpathText.AddString(strText, pic_font.FontFamily, CInt(pic_font.Style), _
emsize, New RectangleF(ptText.X, ptText.Y, szText.Width, szText.Height), _
StringFormat.GenericDefault)
g.DrawImage(CType(bm.Clone, Bitmap), 0, 0)
mx.RotateAt(rAngle, _
New Point(ptText.X + (szText.Width / 2), ptText.Y + (szText.Height / 2)))
mx.TransformPoints(ptsText)
mx.TransformPoints(ptsRotateText)
mx.TransformPoints(ptsAngle)
g.Transform = mx
g.FillPath(br, gpathText)
If chkAddOutline.Checked Then
Using pn As Pen = New Pen(Color.FromArgb(tbarTrans.Value, Color.White), 3)
g.DrawPath(pn, gpathText)
End Using
End If
If MouseOver Then
g.ResetTransform()
g.DrawPolygon(ptsTextPen, ptsText)
g.FillPolygon(New SolidBrush(Color.FromArgb(100, Color.White)), ptsRotateText)
End If
e.Graphics.DrawImage(tbm, 10, 10)
g.Dispose()
mx.Dispose()
br.Dispose()
gpathText.Dispose()
End Sub
Private Sub TrackBar_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tbarTrans.Scroll
lblOpacity.Text = tbarTrans.Value
Me.Invalidate()
End Sub
Sub SetptsText()
ptsText = New PointF() { _
ptText, _
New Point(CInt(ptText.X + szText.Width), ptText.Y), _
New Point(CInt(ptText.X + szText.Width), CInt(ptText.Y + szText.Height)), _
New Point(ptText.X, CInt(ptText.Y + szText.Height)) _
}
ptsRotateText = New PointF() { _
New Point(CInt(ptText.X + szText.Width - 10), ptText.Y), _
New Point(CInt(ptText.X + szText.Width), ptText.Y), _
New Point(CInt(ptText.X + szText.Width), CInt(ptText.Y + 10)), _
New Point(CInt(ptText.X + szText.Width - 10), CInt(ptText.Y + 10)) _
}
ptsAngle = New PointF() {New PointF(CInt(ptText.X + szText.Width), CInt(ptText.Y + (szText.Height / 2)))}
End Sub
Private Sub chkAddOutline_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAddOutline.CheckedChanged
Me.Invalidate()
End Sub
Private Sub cboFontSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFontSize.SelectedIndexChanged
pic_font = New Font("Arial Black", CSng(cboFontSize.Text), FontStyle.Regular, GraphicsUnit.Pixel)
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
Me.Invalidate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
tbm.Save("C:\Test.bmp")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
pic_font = FontDialog1.Font
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
Me.Invalidate()
End If
End Sub
End Class
|
|
|
|
 |
|
 |
This is what I was exactly working today and thanks for the nice article and still I need to do the same for resizing the text.
|
|
|
|
 |
|
 |
Dragging the corner of the rotated box was a difficult challenge. I eventually got it and works great, but it is too involved for a posting. I hope to make an article on it, just don't know how soon I can.
SSDiver2112
|
|
|
|
 |
|
 |
Hi,
It's really nice and I would like to know how to save the image with the rotated text as in this you have taken the image dynamically and I want it to add an image and rotate as you did it and want to save the output.(ie like a watermark).
|
|
|
|
 |
|
 |
Move the Dim statement outside of the routine, and remove the Dispose statement.
Dim tbm As Bitmap
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
tbm = CType(bm.Clone, Bitmap)
.
.
.
tbm.Dispose()
Then Add a Button and Save the picture in the the Click Event.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
tbm.Save("C:\Test.bmp")
End Sub
|
|
|
|
 |
|
 |
Thanks for your quick response and it works perfect.And I need a little help in this program is that straight away I want to load image and rotate text as what you have done in this program instead of paint text and rotate.
And I am pasting my code here can you help me in this
Dim opac As Integer = 0
Dim sOpacity As String = TxtOpacity.Text
Select Case (sOpacity)
Case "100%"
opac = 255 Case "75%"
opac = 191 Case "50%"
opac = 127 Case "25%"
opac = 64 Case "10%"
opac = 25 Case Else
opac = 127 End Select
Dim img As New Bitmap(imgPic.Image) Dim g = Graphics.FromImage(img)
Dim myBrush As Brush
myBrush = New SolidBrush(Color.FromArgb(opac, KryptonColorButton1.SelectedColor))
Dim sz As SizeF = g.MeasureString(TxtImg.Text, FontDialog1.Font)
Dim phWidth As Integer = img.Width
Dim phHeight As Integer = img.Height
If CUShort(Math.Truncate(sz.Width)) < CUShort(phWidth) Then
End If
Dim yPixlesFromBottom As Integer = 0
Dim yPosFromBottom As Single = 0
Dim xCenterOfImg As Single = 0
If BottomCenter.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.05))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (phWidth \ 2)
If X = 0 And Y = 0 Then
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 700)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 735)
g.RotateTransform(-45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(380, 760)
g.RotateTransform(-90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(640, 760)
g.RotateTransform(-180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(450, 490)
g.RotateTransform(-270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 700)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
Else
If BottomLeft.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.05))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (sz.Width / 2) + 10
If X = 0 And Y = 0 Then
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 700)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 735)
g.RotateTransform(-45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 735)
g.RotateTransform(-90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(280, 760)
g.RotateTransform(-180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(60, 500)
g.RotateTransform(-270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 700)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
Else
If BottomRight.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.05))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (phWidth - (sz.Width / 2)) - 10
If X = 0 And Y = 0 Then
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(730, 700)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(730, 735)
g.RotateTransform(-45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(750, 760)
g.RotateTransform(-90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(1010, 760)
g.RotateTransform(-180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(820, 490)
g.RotateTransform(-270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(730, 700)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
Else
If MiddleCenter.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.5))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (phWidth \ 2)
If X = 0 And Y = 0 Then
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(380, 355)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 355)
g.RotateTransform(45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(440, 370)
g.RotateTransform(90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(650, 410)
g.RotateTransform(180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(380, 640)
g.RotateTransform(270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(380, 355)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
Else
If MiddleLeft.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.5))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (sz.Width / 2) + 10
If X = 0 And Y = 0 Then
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 365)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(30, 355)
g.RotateTransform(45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(50, 370)
g.RotateTransform(90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(280, 420)
g.RotateTransform(180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(20, 650)
g.RotateTransform(270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 300)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
Else
If MiddleRight.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.5))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (phWidth - (sz.Width / 2)) - 10
If X = 0 And Y = 0 Then
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(740, 365)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(770, 355)
g.RotateTransform(45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(820, 360)
g.RotateTransform(90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(1010, 410)
g.RotateTransform(180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(760, 640)
g.RotateTransform(270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(750, 300)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
Else
If TopCenter.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.95))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (phWidth \ 2)
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 0)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 0)
g.RotateTransform(45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 0)
g.RotateTransform(90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(650, 60)
g.RotateTransform(180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 280)
g.RotateTransform(270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(400, 0)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
Else
If TopLeft.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.95))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (sz.Width / 2) + 10
If X = 0 And Y = 0 Then
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 10)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(40, 0)
g.RotateTransform(45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(60, 10)
g.RotateTransform(90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(280, 70)
g.RotateTransform(180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 300)
g.RotateTransform(270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(10, 10)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
Else
If TopRight.Checked = True Then
yPixlesFromBottom = CInt(Math.Truncate(phHeight * 0.95))
yPosFromBottom = ((phHeight - yPixlesFromBottom) - (sz.Height / 2))
xCenterOfImg = (phWidth - (sz.Width / 2)) - 10
If cboAngle.SelectedIndex = 0 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(750, 10)
g.RotateTransform(0)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 1 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(760, 10)
g.RotateTransform(45)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 2 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(780, 10)
g.RotateTransform(90)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 3 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(1010, 70)
g.RotateTransform(-180)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 4 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(750, 280)
g.RotateTransform(270)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
If cboAngle.SelectedIndex = 5 Then
g.SmoothingMode = SmoothingMode.HighQuality
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
g.TranslateTransform(750, 10)
g.RotateTransform(360)
g.DrawString(TxtImg.Text, FontDialog1.Font, myBrush, New Point(X, Y))
g.ResetTransform()
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Dim StrFormat As New StringFormat()
StrFormat.Alignment = StringAlignment.Center
imgPreview.Image = img
imgPic.Hide()
imgPreview.Show()
modified on Monday, May 30, 2011 5:15 AM
|
|
|
|
 |
|
 |
I am not exactly sure what you are asking. Are you saying you want to save the image such that when you load it back in the text is separate from the background image and can be edited still, or is it something else?
SSDiver2112
|
|
|
|
 |
|
 |
No sir,actually I am working on watermarking software so that I need to load some pic's and watermark text on the images.In that case I need the text on the image to be roated and resized as you have done in this article.But in this article you have done by taking the image dynamically.But in my case is that I need to load the mage to the picturebox and watermark the text on that image and rotate and resize it.
|
|
|
|
 |
|
 |
In the example remove bm = My.Resources.DivePic
Add an OpenFileDialog and a 'Load' Button and use bm = Image.FromFile(OpenFileDialog1.FileName) to load whatever picture you need.
SSDiver2112
|
|
|
|
 |
|
 |
Sorry Sir I couldnt find the mistake where I am going wrong.
Here I have taken two buttons:One is or opening an image and load it to the picturebox and the other is for saving the image.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
bm = Image.FromFile(OpenFileDialog1.FileName)
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
ptsTextPen.DashStyle = DashStyle.Dot
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
bm.Save(SaveFileDialog1.FileName)
End If
End Sub
And I am getting error while executing at this location:
gp.AddPolygon(ptsText)
which is in the mouseovertext event
Error:Value cannot be null
And when I am loading the image I cannot find any text over it and I am getting diagonal lines on the form.
modified on Tuesday, May 31, 2011 12:07 PM
|
|
|
|
 |
|
 |
I added your load event and do not get the error. You must have customized the code somewhere causing it. You should trace the code to see why you are getting a NULL value.
I need more detail on your loading problem.
SSDiver2112
|
|
|
|
 |
|
 |
Here is the entire code sir:
Imports System.Collections.ObjectModel
Imports System.Drawing.Drawing2D
Public Class Form1
Dim pic_font As New Font("Arial Black", 40, FontStyle.Regular, GraphicsUnit.Pixel)
Dim bm As Bitmap
Dim strText As String = "Diver Dude"
Dim szText As New SizeF
Dim ptText As New Point(125, 125)
Dim ptsText() As PointF
Dim MovingOffset As PointF
Dim ptsTextPen As Pen = New Pen(Color.LightSteelBlue, 1)
Dim MouseMoving As Boolean
Dim MouseOver As Boolean
Public Sub New()
MyBase.New()
InitializeComponent()
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
ptsTextPen.DashStyle = DashStyle.Dot
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If IsMouseOverText(e.X - 10, e.Y - 10) Then
MouseMoving = True
MovingOffset.X = e.X - ptText.X
MovingOffset.Y = e.Y - ptText.Y
Else
MouseMoving = False
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If IsMouseOverText(e.X - 10, e.Y - 10) Then
If Not MouseOver Then
MouseOver = True
Me.Refresh()
End If
Else
If MouseOver Then
MouseOver = False
Me.Refresh()
End If
End If
If e.Button = Windows.Forms.MouseButtons.Left And MouseMoving Then
ptText.X = CInt(e.X - MovingOffset.X)
ptText.Y = CInt(e.Y - MovingOffset.Y)
Me.Refresh()
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
MouseMoving = False
Me.Refresh()
End Sub
Public Function IsMouseOverText(ByVal X As Integer, ByVal Y As Integer) As Boolean
Using gp As New GraphicsPath()
gp.AddPolygon(ptsText)
Using TextRegion As New Region(gp)
Return TextRegion.IsVisible(X, Y)
End Using
End Using
End Function
Dim tbm As Bitmap
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
tbm = CType(bm.Clone, Bitmap)
Dim g As Graphics = Graphics.FromImage(tbm)
Dim mx As Matrix = New Matrix
Dim gpathText As New GraphicsPath
Dim br As SolidBrush = New SolidBrush(Color.FromArgb(tbarTrans.Value, _
Color.LightCoral))
SetptsText()
g.SmoothingMode = SmoothingMode.AntiAlias
Dim emsize As Single = Me.CreateGraphics.DpiY * pic_font.SizeInPoints / 72
gpathText.AddString(strText, pic_font.FontFamily, CInt(pic_font.Style), _
emsize, New RectangleF(ptText.X, ptText.Y, szText.Width, szText.Height), _
StringFormat.GenericDefault)
g.DrawImage(CType(bm.Clone, Bitmap), 0, 0)
mx.RotateAt(tbarRotate.Value, _
New Point(ptText.X + (szText.Width / 2), ptText.Y + (szText.Height / 2)))
mx.TransformPoints(ptsText)
g.Transform = mx
g.FillPath(br, gpathText)
If chkAddOutline.Checked Then
Using pn As Pen = New Pen(Color.FromArgb(tbarTrans.Value, Color.White), 1)
g.DrawPath(pn, gpathText)
End Using
End If
If MouseOver Then
g.ResetTransform()
g.DrawPolygon(ptsTextPen, ptsText)
End If
e.Graphics.DrawImage(tbm, 10, 10)
g.Dispose()
mx.Dispose()
br.Dispose()
gpathText.Dispose()
End Sub
Private Sub TrackBar_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tbarRotate.Scroll, tbarTrans.Scroll
lblRotate.Text = tbarRotate.Value
lblOpacity.Text = tbarTrans.Value
Me.Refresh()
End Sub
Sub SetptsText()
ptsText = New PointF() { _
ptText, _
New Point(CInt(ptText.X + szText.Width), ptText.Y), _
New Point(CInt(ptText.X + szText.Width), CInt(ptText.Y + szText.Height)), _
New Point(ptText.X, CInt(ptText.Y + szText.Height)) _
}
End Sub
Private Sub chkAddOutline_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAddOutline.CheckedChanged
Me.Refresh()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
bm.Save(SaveFileDialog1.FileName)
End If
End Sub
End Class
I am getting confused in the form load event if there is no image when the form loaded the application hangs due to the paint method.
|
|
|
|
 |
|