Well I solved it.
windows forms) to draw to Bitmap too.
Post by Mick DohertyPost by Lloyd DupontI tried that, which seems reasonable though a tad acrobatic but, quite
strangely, I had no GDI output at all when doing that ?!?!
Sounds like you either missed out the BitBlt, or the SelectObject
afterwards.
I am currently doing something very similar (in VB.Net though) and it
works exactly as expected.
Post by Lloyd DupontAnyway, I dropped this issue as it is quite marginal and I could make
sure to use ANTIALIAS_QUALITY in such case.
Personally I always like to know why something didn't work, even if I have
an acceptable workaround.
So in case it helps here are the exact methods that I am using along with
(Interop declarations not included)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'You sometimes need to use GDI since the control whos surface
'you're drawing to uses GDI to measure the rectangle and the GDI+
'DrawString method results in a string that's too long to fit.
Private Sub DrawGDIText(ByVal hdc As IntPtr, ByVal text As String, _
ByVal font As Font, ByVal bounds As Rectangle, _
ByVal backColor As Color, _
ByVal foreColor As Color, _
ByVal alignment As ContentAlignment)
SetTextColor(hdc, ColorTranslator.ToWin32(foreColor))
If backColor.Equals(Color.Transparent) Then
SetBkMode(hdc, TRANSPARENT)
Else
SetBkMode(hdc, OPAQUE)
SetBkColor(hdc, ColorTranslator.ToWin32(backColor))
End If
Dim hFont As IntPtr = font.ToHfont
Dim hOldFont As IntPtr = SelectObject(hdc, hFont)
Dim flags As Int32 = DT_SINGLELINE
Select Case alignment
Case ContentAlignment.TopLeft
flags += DT_TOP Or DT_LEFT
Case ContentAlignment.TopCenter
flags += DT_TOP Or DT_CENTER
Case ContentAlignment.TopRight
flags += DT_TOP Or DT_RIGHT
Case ContentAlignment.MiddleLeft
flags += DT_VCENTER Or DT_LEFT
Case ContentAlignment.MiddleCenter
flags += DT_VCENTER Or DT_CENTER
Case ContentAlignment.MiddleRight
flags += DT_VCENTER Or DT_RIGHT
Case ContentAlignment.BottomLeft
flags += DT_BOTTOM Or DT_LEFT
Case ContentAlignment.BottomCenter
flags += DT_BOTTOM Or DT_CENTER
Case ContentAlignment.BottomRight
flags += DT_BOTTOM Or DT_RIGHT
End Select
DrawText(hdc, text, -1, New RECT(bounds), flags)
SelectObject(hdc, hOldFont)
DeleteObject(hFont)
End Sub
'A bitmap needs to pass a CompatibleDC to the DrawText method.
Friend Sub DrawGDIText(ByVal bm As Bitmap, ByVal text As String, _
ByVal font As Font, ByVal bounds As Rectangle, _
ByVal backColor As Color, _
ByVal foreColor As Color, _
ByVal alignment As ContentAlignment)
Dim g As Graphics = Graphics.FromImage(bm)
If Not backColor.Equals(Color.Transparent) Then
Dim backBrush As New SolidBrush(backColor)
g.FillRectangle(backBrush, bounds)
backBrush.Dispose()
End If
Dim hDC As IntPtr = g.GetHdc
Dim cDC As IntPtr = CreateCompatibleDC(hDC)
Dim hBitmap As IntPtr = SelectObject(cDC, bm.GetHbitmap)
DrawGDIText(cDC, text, font, bounds, Color.Transparent, _
foreColor, alignment)
BitBlt(hDC, bounds.Left, bounds.Top, bounds.Width, bounds.Height, _
cDC, bounds.Left, bounds.Top, SRCCOPY)
SelectObject(cDC, hBitmap)
DeleteDC(cDC)
g.ReleaseHdc(hDC)
g.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim bm As New Bitmap(someImage)
Dim testFont As New Font("Times New Roman", 36, _
FontStyle.Italic, GraphicsUnit.Point)
DrawGDIText(bm, "Sample", testFont, _
New Rectangle(Point.Empty, bm.Size), _
Color.FromArgb(200, 255, 128, 0), Color.White, _
ContentAlignment.MiddleCenter)
testFont.Dispose()
bm.Save("c:\Testing.png", Imaging.ImageFormat.Png)
bm.Dispose()
End Sub
/////////////////////////////////////////////////////////////////////////////
Post by Lloyd DupontRegards,
Lloyd
PS: I upgraded to the latest version of Nothing and it really works just
as expected!
Without all these annoying spywares and adwares you find in Anything
nowadays!
:-) I'll add that to my feedback
--
Mick Doherty
http://dotnetrix.co.uk/nothing.html