Draw Line and Text on Bitmap Image in C#
Hello Friends ,
This
is a sample Class example of C#,using this code you can Draw
the text and line using Graphics pen on Bitmap images.
Sample
example code (C# Language - VS.Net)
----------------------------------------------------------------------------------------------------------using System;
using
System.Drawing;
using
System.IO;
using
System.Diagnostics;
using
System.Data;
using
System.Drawing.Imaging;
using
System.Threading;
using
System.Collections;
using
System.Globalization;
using
System.Windows.Forms;
using
System.Configuration;
using
System.Net;
using
System.Net.Mail;
using
System.Net.NetworkInformation;
namespace
MyTest
{
public
class
GenerateImage
{
private
Thread[]
threads;
private
System.Collections.Hashtable
hashConnectionInfo = new
System.Collections.Hashtable();
public
string
appPath = "E:\\MyPenImage\\";
public
GenerateImage()
{
}
private
static
ImageCodecInfo
GetEncoderInfo(String
mimeType)
{
int
j;
ImageCodecInfo[]
encoders;
encoders =
ImageCodecInfo.GetImageEncoders();
for
(j = 0; j < encoders.Length; ++j)
{
if
(encoders[j].MimeType == mimeType)
return
encoders[j];
}
return
null;
}
public
void
GenerateImageFromPenData()
{
String
PenData = "Cordinates
of X1,Y1,X2,Y2";
Bitmap
bm;
bm = new
Bitmap(appPath
+ "\\BackGround\\Page.png");
Graphics
g = Graphics.FromImage(bm);
uint
color = 9109545;
uint
blue = (color & 0x00ff0000) >> 16;
uint
green = (color & 0x0000ff00) >> 8;
uint
red = (color & 0x000000ff);
Pen
pen = new
System.Drawing.Pen(Color.FromArgb((int)red,
(int)green,
(int)blue),
1);
// Pen
pen = new System.Drawing.Pen(Color.Blue, 1);
int
strokesCount = PenData.Split(',').Length;
int
DRAWSCALE = 1;
g.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
string[]
arrPenData;
arrPenData =
PenData.Split(',');
for
(int
i = 0; i < arrPenData.Length; i = i + 2)
{
float
x, y, x1, y1;
x =
float.Parse(arrPenData[i].Split('
')[0]);
y =
float.Parse(arrPenData[i].Split('
')[1]);
x1 =
float.Parse(arrPenData[i
+ 1].Split('
')[0]);
y1 =
float.Parse(arrPenData[i
+ 1].Split('
')[1]);
//MessageBox.Show(x+"
"+y+" "+x1+" "+y1);
g.DrawLine(pen,
(Convert.ToSingle(x)
/ DRAWSCALE), (Convert.ToSingle(y)
/ DRAWSCALE)
,
(Convert.ToSingle(x1))
/ DRAWSCALE, (Convert.ToSingle(y1))
/ DRAWSCALE);
}
string
pageName = "A"
+ "_"
+ "B"
+ "_"
+ DateTime.Now.ToString("yyyyMMdd_HHmmss")
+ "."
+ ("test").Replace('.',
'_');
String
ImageText = "Signature";
if
(ImageText != "")
{
Point
pLoc = new
Point(255,
906);
g.DrawString(ImageText
+ "
"
+ DateTime.Now.ToString("dd-MMM-yyyy
HH:mm:ss"),
new
Font("Arial",
10, FontStyle.Regular),
new
SolidBrush(Color.Black),
pLoc);
g.DrawString("Page
Number: "
+ "1",
new
Font("Arial",
10, FontStyle.Regular),
new
SolidBrush(Color.Black),
new
Point(25,
906));
}
Encoder
Enc = Encoder.SaveFlag;
EncoderParameters
EncParms = new
EncoderParameters(1);
EncoderParameter
EncParm;
ImageCodecInfo
CodecInfo = GetEncoderInfo("image/jpeg");
EncParm = new
EncoderParameter(Encoder.Quality,
(long)300);
EncParms.Param[0] =
new
EncoderParameter(Encoder.Quality,
(long)300);
bm.Save(appPath +
pageName + ".jpg",
CodecInfo, EncParms);
}
}
}
Comments
Post a Comment