Created On: 03 July 2012
Problem:
How can I print the contents of an image file like a .BMP or .GIF to the default printer at a specified location on the page using Visual COBOL and the .NET Framework?
Resolution:
The following example will do exactly that. There are two sets of code that can be used to either print an image residing in a file on disk or an image that resides in a resource file that is part of the project.
The entire solution is attached. When unzipping the file please do so to your C:\ drive retaining the folder structure that is in the .zip file.
$set ilusing"System.Drawing.Printing"
$set ilusing"printimagevc.Properties"
class-id printimagevc.Form1 is partial
inherits type System.Windows.Forms.Form.
working-storage section.
method-id NEW.
procedure division.
invoke self::InitializeComponent
goback.
end method. method-id button1_Click final private.
01 pd type PrintDocument.
procedure division using by value sender as object e as type System.EventArgs.
set pd to new PrintDocument
invoke pd::add_PrintPage(new System.Drawing.Printing.PrintPageEventHandler (self::pDoc_PrintPage))
invoke pd::Print
goback.
end method.
01 img type Image.
01 loc type Point.
procedure division using by value sender as object, e as type PrintPageEventArgs.
*> If loading image directly from file use the following.
set img to type Image::FromFile("C:\printimagevc\LOGO.GIF")
set loc to new Point(100, 100)
invoke e::Graphics::DrawImage(img, loc)
*> If printing an image that is part of the projects resource file uncomment the line below and comment the previous
*> 3 lines.
*> invoke e::Graphics::DrawImage(type Resources::LOGO, 100, 100)
goback.
end method.
end class.
The entire solution is attached. When unzipping the file please do so to your C:\ drive retaining the folder structure that is in the .zip file.
$set ilusing"System.Drawing.Printing"
$set ilusing"printimagevc.Properties"
class-id printimagevc.Form1 is partial
inherits type System.Windows.Forms.Form.
working-storage section.
method-id NEW.
procedure division.
invoke self::InitializeComponent
goback.
end method. method-id button1_Click final private.
01 pd type PrintDocument.
procedure division using by value sender as object e as type System.EventArgs.
set pd to new PrintDocument
invoke pd::add_PrintPage(new System.Drawing.Printing.PrintPageEventHandler (self::pDoc_PrintPage))
invoke pd::Print
goback.
end method.
method-id pDoc_PrintPage final private.
01 img type Image.
01 loc type Point.
procedure division using by value sender as object, e as type PrintPageEventArgs.
*> If loading image directly from file use the following.
set img to type Image::FromFile("C:\printimagevc\LOGO.GIF")
set loc to new Point(100, 100)
invoke e::Graphics::DrawImage(img, loc)
*> If printing an image that is part of the projects resource file uncomment the line below and comment the previous
*> 3 lines.
*> invoke e::Graphics::DrawImage(type Resources::LOGO, 100, 100)
goback.
end method.
end class.