.NET Framework Bookmark and Share   
 index > .NET Base Class Library > How to store and clone PrinterSettings?
 

How to store and clone PrinterSettings?

Hello all

1)

I need to adjust printer settings by PrintDialog and the to store them for later use. It is an adjustmentfor applications parameters. These parameters are later used for printing. How can I save and restore the printer setting objectincluding the necessary subitems. In other words the full information needed for printing.

2)

How can I clone the PrinterSettings object. If I use the Clone() method the object is cloned without the DefaultPrinterSettings and may be other fields. If I do my own clone function I cannot set the DefaultPrinterSettings because it is read only.

I hope on your help.

Reiner

Reiner Obrecht

I used this code:

Code Snippet

Imports System.Drawing.Printing

Imports System.Runtime.Serialization.Formatters

Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

PrintDialog1.ShowDialog()

Dim FS As New FileStream("C:\Test.dat", FileMode.OpenOrCreate)

Dim BF As New Binary.BinaryFormatter

BF.Serialize(FS, PrintDialog1.PrinterSettings.Clone)

FS.Close()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

PrintDialog1.ShowDialog()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim FS As New FileStream("C:\test.dat", FileMode.Open)

Dim BF As New Binary.BinaryFormatter

PrintDialog1.PrinterSettings = DirectCast(BF.Deserialize(FS), PrinterSettings)

FS.Close()

End Sub

End Class

To test serialization, saving and restoring the printer settings. It worked well for me.

JohnWein

I don't see the problem. Using the following code:

Code Snippet

Private SavedPrinterSetting As New PrinterSettings

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

PrintDialog1.ShowDialog()'Change some settings.

SavedPrinterSetting = PrintDialog1.PrinterSettings.Clone

PrintDialog1.ShowDialog() 'Change some settings.

PrintDialog1.PrinterSettings = SavedPrinterSetting

PrintDialog1.ShowDialog()'Shows saved settings.

End Sub

I can save, change the PrinterSettings and then restore the saved PrinterSettings without problem.

JohnWein

Hello John and thanks for your fast response.

1)

Maybe my question was not clearly. The first question means, how can I save the PrinterSettings to HDD in order to create a new PrinterSettings object at next application start without having to open the PrintDialog again. I tried it by serializing the members of PrinterSettings. But I cannot access all data, because something is stored private.

2)

For the cloning problem try the following.
- Open, edit and close a PrintDialog
- Assign the PrinterSettings from the dialog to a local variable x.
- Create a new PrintDialog and assign a clone of the local variable xto its printer settings.
- Make any changeswithin the new dialog

If you attach a debugger, you will see that the changes do also affect the original local variable x, which means the clone was not a complete clone.

Reiner Obrecht

Reiner Obrecht

I used this code:

Code Snippet

Imports System.Drawing.Printing

Imports System.Runtime.Serialization.Formatters

Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

PrintDialog1.ShowDialog()

Dim FS As New FileStream("C:\Test.dat", FileMode.OpenOrCreate)

Dim BF As New Binary.BinaryFormatter

BF.Serialize(FS, PrintDialog1.PrinterSettings.Clone)

FS.Close()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

PrintDialog1.ShowDialog()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim FS As New FileStream("C:\test.dat", FileMode.Open)

Dim BF As New Binary.BinaryFormatter

PrintDialog1.PrinterSettings = DirectCast(BF.Deserialize(FS), PrinterSettings)

FS.Close()

End Sub

End Class

To test serialization, saving and restoring the printer settings. It worked well for me.

JohnWein
Thank you! Your answer helped me solving this problem: PrintDialog used with certain drivers does not save correctly some settings like custom paper sizes, BUT if I save PrinterSettings into a variable by myself, it works!

Nico60

You can use google to search for other answers

Custom Search

More Threads

• Problem with BackgroundWorker
• Designer Serialization and Localization
• Custom Data Types
• Reading binary file
• future class design
• Search a .zip file for a file name?
• Decompress file compressed via Compress.exe
• Working with a user input?
• Random Nage Generator - I got a Problem!
• Type Casting in CodeDom