Home All Groups Group Topic Archive Search About
Author
3 Jun 2009 9:53 AM
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

Author
4 Jun 2009 8:52 AM
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:
msd***@microsoft.com.
Author
8 Jun 2009 12:29 AM
Steve
Hi Colbert

Thanks for the reply

Your code example worked at treat

Thanks again

regards
Steve


Show quoteHide quote
"Colbert Zhou [MSFT]" <colbe***@online.microsoft.com> wrote in message
news:hClcXHP5JHA.7260@TK2MSFTNGHUB02.phx.gbl...
> 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:
> msd***@microsoft.com.
>
Author
8 Jun 2009 8:35 AM
Olaf Rabbachin
Hi,

Colbert Zhou [MSFT] wrote:

>         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