Question Hdc -Can anyone tell me wots wrong with my code?

GBMSoft

New member
Joined
Jul 1, 2013
Messages
4
Programming Experience
3-5
Public Sub ToImage(ByVal cntrl As RichTextBox, ByVal filename As String, ByVal inputformat As Imaging.PixelFormat, ByVal outputformat As Imaging.ImageFormat)




Dim charfrom As Integer, charto As Integer


' Mark starting and ending character
Dim cRange As CHARRANGE
cRange.cpMin = charFrom
cRange.cpMax = charTo


' Calculate the area to render and print
Dim rectToPrint As RECT
rectToPrint.Top = CInt(cntrl.Top * AnInch)
rectToPrint.Bottom = CInt(cntrl.Bottom * AnInch)
rectToPrint.Left = CInt(cntrl.Left * AnInch)
rectToPrint.Right = CInt(cntrl.Right * AnInch)


Dim newImage As New Bitmap(cntrl.ClientRectangle.Width, cntrl.ClientRectangle.Height + 20, inputformat)


' Dim newImage As New Bitmap(cntrl.ClientRectangle.Width, cntrl.ClientRectangle.Height + 20, inputformat)
Dim gr As Graphics = Graphics.FromImage(newImage)
Using gr
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
gr.Clear(Color.Transparent)


Dim hdc As IntPtr = gr.GetHdc()


Dim fmtRange As FORMATRANGE
fmtRange.chrg = cRange ' Indicate character from to character to
fmtRange.hdc = hdc ' Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc ' Point at printer hDC
fmtRange.rc = rectToPrint ' Indicate the area on page to print


Dim res As IntPtr = IntPtr.Zero


Dim wparam As IntPtr = IntPtr.Zero
wparam = New IntPtr(1)


' Move the pointer to the FORMATRANGE structure in memory
Dim lparam As IntPtr = IntPtr.Zero
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, lparam, False)


' Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)


' Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam)


' Release the device context handle obtained by a previous call
res = SendMessage(Handle, EM_FORMATRANGE, wparam, New IntPtr(0))


gr.ReleaseHdc(hdc)


End Using


newImage.Save(filename, outputformat)
newImage.Dispose()




End Sub
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Sorry, I have not looked at your code since it should not be up to us to figure out what is wrong with the code when we have no idea what it is supposed to be doing in the first place and what went wrong.

So, do yourself a big favour and start again. Tell us what you are wanting to do and what you are expecting to happen and then tell us what actually happened and what went wrong. If you got errors, then where did they occur and what were the error messages that were generated.

Until you make the effort to help us then you will find that people will be reluctant to help you.

Good luck, welcome to the Forum, and hope that helps.

Cheers,

Ian
 

GBMSoft

New member
Joined
Jul 1, 2013
Messages
4
Programming Experience
3-5
Hi,

Sorry, I have not looked at your code since it should not be up to us to figure out what is wrong with the code when we have no idea what it is supposed to be doing in the first place and what went wrong.

So, do yourself a big favour and start again. Tell us what you are wanting to do and what you are expecting to happen and then tell us what actually happened and what went wrong. If you got errors, then where did they occur and what were the error messages that were generated.

Until you make the effort to help us then you will find that people will be reluctant to help you.

Good luck, welcome to the Forum, and hope that helps.

Cheers,

Ian


hi ian

my apologies

the code saves a richtextbox controls data to a bitmap

the problem is it just gives me a black bitmap

see attached, beet at this 2 days, no luck??

Please help!!
 

Attachments

  • output.jpg
    output.jpg
    4 KB · Views: 37

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,558
Location
Norway
Programming Experience
10+
Useful article: Getting WYSIWYG Print Results from a .NET RichTextBox

In your code you use "Handle", correct handle is the one of the RichTextBox control, ie cntrl.Handle. Compared to that article you see they use just Handle because that is an instance method for an inherited RichTextBox control, so Handle there refers to the current instance of the RTB control.

You have not set values for charfrom and charto, so your cpMin=0 and cpMax=0. Set cpMax=-1 if you want to include all chars (as per documentation).
 

GBMSoft

New member
Joined
Jul 1, 2013
Messages
4
Programming Experience
3-5
Useful article: Getting WYSIWYG Print Results from a .NET RichTextBox

In your code you use "Handle", correct handle is the one of the RichTextBox control, ie cntrl.Handle. Compared to that article you see they use just Handle because that is an instance method for an inherited RichTextBox control, so Handle there refers to the current instance of the RTB control.

You have not set values for charfrom and charto, so your cpMin=0 and cpMax=0. Set cpMax=-1 if you want to include all chars (as per documentation).



hi john

thanks for you en genius reply...(really it was!)

my next question?

when u use the handle with the printeventargs you can get very clear
quality sent your printer but quality is very bad for a image??
 

Attachments

  • rtf.png
    rtf.png
    9.7 KB · Views: 39

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,558
Location
Norway
Programming Experience
10+
I don't see any quality degration. The unmanaged code renders equally to any DC, as such you can't change anything by setting properties of the Graphics object.
What you're seeing could be artifacts of the image format you're saving to or possibly zooming of large images.
 

GBMSoft

New member
Joined
Jul 1, 2013
Messages
4
Programming Experience
3-5
done.jpgthanks john

your're right again the image was being stretched, this is what happens the code is basically the same as provided by MS for printing rtf text
i want to print the contents of the richtextbox to a jpeg using this method and not the drawstring method as i will lose text formatting

the question is how do i stretch the richtextbox contents on the jpeg so it does not deteriorate the quality.?

here is my latest code::


'a4 page size
Dim newImage As New Bitmap(CInt(2480), CInt(3508), Imaging.PixelFormat.Format32bppArgb)


newImage.SetResolution(300, 300)
Dim gr As Graphics = Graphics.FromImage(newImage)


Using gr
gr.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gr.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
gr.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy
gr.Clear(Color.White)


Dim hdc As IntPtr = gr.GetHdc()


Dim fmtRange As FORMATRANGE
fmtRange.chrg.cpMin = 0
fmtRange.chrg.cpMax = Me.Text.Length
fmtRange.hdc = hdc
fmtRange.hdcTarget = hdc
fmtRange.rc = rectToPrint
fmtRange.rcPage = rectToPrint


Dim res As IntPtr = IntPtr.Zero


Dim wparam As IntPtr = IntPtr.Zero
wparam = New IntPtr(1)


' Move the pointer to the FORMATRANGE structure in memory
Dim lparam As IntPtr = IntPtr.Zero
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, lparam, False)




' Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)


Marshal.FreeCoTaskMem(lparam)


'cancels print
e.Cancel = True


res = SendMessage(Handle, EM_FORMATRANGE, wparam, New IntPtr(0))


gr.ReleaseHdc(hdc)




newImage.Save("d:\desktop\done.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
newImage.Dispose()


End Using



thank you for your help thus far. it is much appreciated.
 
Top Bottom