|
We start with our source System.Drawing code.
[C#]
using System;
using System.IO;
using System.Reflection;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
...
// create a canvas for painting on
Bitmap pg = new Bitmap((int)(8.5 * 72), (int)(11 * 72));
Graphics gr = Graphics.FromImage(pg);
// clear the canvas to white
Rectangle pgRect = new Rectangle(0, 0, pg.Width, pg.Height);
SolidBrush solidWhite = new SolidBrush(Color.White);
gr.FillRectangle(solidWhite, pgRect);
// load a new image and draw it centered on our canvas
Stream stm =
Assembly.GetExecutingAssembly().GetManifestResourceStream("Examples.pic1.jpg");
Image img = Image.FromStream(stm);
int w = img.Width * 2;
int h = img.Height * 2;
Rectangle rc = new Rectangle((pg.Width - w) / 2, (pg.Height - h) /
2, w, h);
gr.DrawImage(img, rc);
img.Dispose();
stm.Close();
// frame the image with a black border
gr.DrawRectangle(new Pen(Color.Black, 4), rc);
// add some text at the top left of the canvas
Font fn = new Font("Comic Sans MS", 72);
SolidBrush solidBlack = new SolidBrush(Color.Black);
gr.DrawString("My Picture", fn, solidBlack, (int)(pg.Width * 0.1),
(int)(pg.Height * 0.1));
// save the output
pg.Save("../../abcpdf.drawing.gif",
System.Drawing.Imaging.ImageFormat.Gif);
[Visual Basic]
Imports System.IO
Imports System.Reflection
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
...
' create a canvas for painting on
Dim pg As Bitmap = New Bitmap(CType((8.5 * 72),(Integer)(11 * 72),
Integer))
Dim gr As Graphics = Graphics.FromImage(pg)
' clear the canvas to white
Dim pgRect As Rectangle = New Rectangle(0,0,pg.Width,pg.Height)
Dim solidWhite As SolidBrush = New SolidBrush(Color.White)
gr.FillRectangle(solidWhite, pgRect)
' load a new image and draw it centered on our canvas
Dim stm As Stream =
Assembly.GetExecutingAssembly().GetManifestResourceStream("Examples.pic1.jpg")
Dim img As Image = Image.FromStream(stm)
Dim w As Integer = img.Width * 2
Dim h As Integer = img.Height * 2
Dim rc As Rectangle = New Rectangle((pg.Width - w) / 2,(pg.Height -
h) / 2,w,h)
gr.DrawImage(img, rc)
img.Dispose()
stm.Close()
' frame the image with a black border
gr.DrawRectangle(New Pen(Color.Black,4),rc)
' add some text at the top left of the canvas
Dim fn As Font = New Font("Comic Sans MS",72)
Dim solidBlack As SolidBrush = New SolidBrush(Color.Black)
gr.DrawString("My Picture", fn, solidBlack, CType((pg.Width * 0.1),
(Integer)(pg.Height * 0.1), Integer))
' save the output
pg.Save("../../abcpdf.drawing.gif",
System.Drawing.Imaging.ImageFormat.Gif)
|
|
|