Print in grey scale

S

Steve

Hi All

I am trying to force a colour laser printer to print images in a document in
greyscale, in vb.net 2008 SP1

code......
Dim printDoc As New PrintDocument()

printDoc.PrinterSettings.PrinterName = printername

If flgblackandwhite AndAlso printDoc.PrinterSettings.SupportsColor Then

printDoc.DefaultPageSettings.Color = False

AddHandler printDoc.PrintPage, AddressOf PrintPage

printdoc.print

End If

This doesn't work. any images still print in colour

I have stepped thru the code and the printDoc.DefaultPageSettings.Color =
false is definitely set

If I set the printer options in windows xp printers and faxes to print grey
scale it works fine

Any ideas

Regards

Steve
 
C

Colbert Zhou [MSFT]

Hello Steve,

The codes look fine to me. I am not sure if the issue is specific to
printers. But by the way, if you set the e.PageSettings.Color to false in
the PrintPage event handler. Does it fix the issue?

Another point to mention, even if the PageSettings.Color works, it should
set the printout in black/while. The black/white effect is different from
the gray scale you desired. So, if you can console the generation of the
printout image, a better recommendation is converting the image to gray
scale using codes before printing. The following codes do this for me,

Public Class Form1
Public Shared Function MakeGrayscale(ByVal original As Bitmap) As Bitmap
Dim newBitmap As New Bitmap(original.Width, original.Height)
Dim i As Integer
For i = 0 To original.Width - 1
Dim j As Integer
For j = 0 To original.Height - 1
Dim originalColor As Color = original.GetPixel(i, j)
Dim grayScale As Integer = CInt((((originalColor.R * 0.3) +
(originalColor.G * 0.59)) + (originalColor.B * 0.11)))
Dim newColor As Color = Color.FromArgb(grayScale,
grayScale, grayScale)
newBitmap.SetPixel(i, j, newColor)
Next j
Next i
Return newBitmap
End Function

Private Sub CaptureScreen()
Dim myGraphics As Graphics = MyBase.CreateGraphics
Dim s As Size = MyBase.Size
Me.memoryImage = New Bitmap(s.Width, s.Height, myGraphics)

Graphics.FromImage(Me.memoryImage).CopyFromScreen(MyBase.Location.X,
MyBase.Location.Y, 0, 0, s)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.CaptureScreen()
Me.printDocument1.PrinterSettings.PrinterName = "Microsoft XPS
Document Writer"
Me.printDocument1.Print()
End Sub

Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As
PrintPageEventArgs) Handles printDocument1.PrintPage
e.Graphics.DrawImage(Form1.MakeGrayscale(Me.memoryImage), 0, 0)
End Sub

Private memoryImage As Bitmap
Private WithEvents printDocument1 As PrintDocument = New PrintDocument
End Class

It is a Windows Form application, the CaptureScreen create an image of the
current form. And before I print it, I call MakeGrayscale to convert the
image and then output it in the PrintPage event handler.

Hope this helps!

Best regards,
Colbert Zhou
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
S

Steve

Hi Colbert

Thanks for the reply

Your code example worked at treat

Thanks again

regards
Steve
 
O

Olaf Rabbachin

Hi,
For i = 0 To original.Width - 1
Dim j As Integer
For j = 0 To original.Height - 1
[...]

it doesn't make much sense (performance-wise) to convert a bitmap pixel by
pixel - a ColorMatrix would be the much better choice.
See Bob's GDI+-FAQ for an exsample: www.bobpowell.net/grayscale.htm

Cheers,
Olaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top