Printing an Image Data

To print an image or text, use a PrintDocument control. To following example shows how to print an image data, using a PrintDocument control.

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. open System.Drawing.Printing  
  10.   
  11. open System.Drawing.Imaging  
  12. let imageform = new Form(Text="Print Form",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  13. //creates our control  
  14. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  15. let loadbutton=new Button(Text="Load", Location=new System.Drawing.Point(120, 200))  
  16. let prnbutton=new Button(Text="Print", Location=new System.Drawing.Point(40, 200))  
  17. let pic=new PictureBox(SizeMode=PictureBoxSizeMode.StretchImage,Location=new System.Drawing.Point(20, 20),BorderStyle=BorderStyle.FixedSingle,Size=new System.Drawing.Size(100, 100))  
  18. let label=new Label(AutoSize=true,Location=new System.Drawing.Point(0, 120))  
  19. let dlg=new OpenFileDialog()  
  20. let gr=imageform.CreateGraphics()  
  21. let prn=new System.Drawing.Printing.PrintDocument()  
  22. imageform .Controls.Add(pic)  
  23. imageform.Controls.Add(loadbutton)  
  24. imageform.Controls.Add(label)  
  25. imageform.Controls.Add(prnbutton)  
  26. imageform.Controls.Add(exitbutton)  
  27. //sends the data to the printer  
  28. prnbutton.Click.Add(fun startprint->prn.Print())  
  29.   
  30. loadbutton.Click.Add(fun load->  
  31. //filter dialog result  
  32. dlg.Filter <- "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif"  
  33. //adds title to your dialog box  
  34.                    dlg.Title<-"Select an Image File"  
  35.                    if dlg.ShowDialog()=DialogResult.OK then  
  36. //creates a bitmap object  
  37. //assigns the image selected by the user as its value  
  38.                       let bmp=new System.Drawing.Bitmap(dlg.FileName)  
  39.                       bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone)  
  40. //assigns the loaded image as a picturebox value  
  41.                       pic.Image<-bmp  
  42. //displays the image url in our label  
  43.                       label.Text<-"\t\tFilename:" + Convert.ToString(Convert.ToChar(32))+ (dlg.FileName))   
  44. //specifies the data to be printed  
  45. //in this case our image                        
  46. prn.PrintPage.Add(fun printdata->gr.DrawImage(pic.Image,10,10))  
  47. //close the form                                                                                                          
  48. exitbutton.Click.Add(fun quit->imageform.Close())                                           
  49. [<stathread>]                      
  50. //executes our application  
  51. Application.Run(imageform)  
  52. </stathread>