C# Graphics绘图 picBox
需求:

Bitmap bm = new Bitmap(picboxPreview.Width, picboxPreview.Height);
using (Graphics g = Graphics.FromImage(bm))
{
//绘制外面的矩形边框
Pen pen = new Pen(Color.Black, 2.0f);
g.DrawRectangle(pen, new Rectangle(new Point(, ), picboxPreview.Size));
//绘制左边的位图
if (cbxPhoto.CheckedStatus && picboxPhoto.Image != null)
{
g.DrawImage(picboxPhoto.Image, new Rectangle(new Point(, ), new Size(picboxPreview.Height - , picboxPreview.Height - )));
}
//绘制右边的位图
if (cbxLogo.CheckedStatus && picboxLogo.Image != null)
{
g.DrawImage(picboxLogo.Image, new Rectangle(new Point(picboxPreview.Width - (picboxPreview.Height - ) - , ), new Size(picboxPreview.Height - , picboxPreview.Height - )));
}
//绘制Name、Designation、Department、Institution/Company
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Font fA = new Font("Arial", , FontStyle.Bold);
Font fB = new Font("Arial", , FontStyle.Regular);
//1.绘制4行 Name Designation Department Institution/Company
//2.绘制3行 Name Designation Department
//3. Name Designation Institution/Company
//4 Name Department Institution/Company
//5 Designation Department Institution/Company
//6.绘制2行 Name Designation
//7 Name Department
//8 Name Institution/Company
//9 Designation Department
//10 Designation Institution/Company
//11 Department Institution/Company
//12.绘制1行Name
//13. Designation
//14. Department
//15. Institution/Company
bool EnableName = cbxName.CheckedStatus && !string.IsNullOrEmpty(txtName.Text.Trim());
bool EnableDesignation = cbxDesignation.CheckedStatus && !string.IsNullOrEmpty(txtDesignation.Text.Trim());
bool EnableDepartment = cbxDepartment.CheckedStatus && !string.IsNullOrEmpty(txtDepartment.Text.Trim());
bool EnableInstitution = cbxInstitution.CheckedStatus && !string.IsNullOrEmpty(txtInstitution.Text.Trim());
//float drawX;
float drawY;
SizeF sizefName = g.MeasureString(txtName.Text.Trim(), new Font("Arial", , FontStyle.Bold));
SizeF sizefDesignation = g.MeasureString(txtDesignation.Text.Trim(), new Font("Arial", , FontStyle.Regular));
SizeF sizefDepartment = g.MeasureString(txtDepartment.Text.Trim(), new Font("Arial", , FontStyle.Regular));
SizeF sizefInstitution = g.MeasureString(txtInstitution.Text.Trim(), new Font("Arial", , FontStyle.Regular));
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
bool EnableRows = EnableName && EnableDesignation && EnableDepartment && EnableInstitution;
if (EnableName && EnableDesignation && EnableDepartment && EnableInstitution)
{//1.绘制4行 Name Designation Department Institution/Company
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDesignation.Height + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && EnableDesignation && EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefDesignation.Height + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && !EnableDesignation && EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefInstitution.Height)) / + sizefName.Height + sizefDesignation.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height)) / + sizefName.Height + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (!EnableName && !EnableDesignation && EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDepartment.Height + sizefInstitution.Height)) / ;
DrawStringOfDepartment(g, sizefDepartment, drawY); drawY = (picboxPreview.Height - (sizefDepartment.Height + sizefInstitution.Height)) / + sizefDepartment.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefInstitution.Height)) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefInstitution.Height)) / + sizefDesignation.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height)) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY); drawY = (picboxPreview.Height - (sizefDesignation.Height + sizefDepartment.Height)) / + sizefDesignation.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (EnableName && !EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefInstitution.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefInstitution.Height)) / + sizefName.Height;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (EnableName && !EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDepartment.Height)) / + sizefName.Height;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (EnableName && EnableDesignation && !EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height)) / ;
DrawStringOfName(g, sizefName, drawY); drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height)) / + sizefName.Height;
DrawStringOfDesignation(g, sizefDesignation, drawY);
}
else if (!EnableName && !EnableDesignation && !EnableDepartment && EnableInstitution)
{
drawY = (picboxPreview.Height - sizefInstitution.Height) / ;
DrawStringOfInstitution(g, sizefInstitution, drawY);
}
else if (!EnableName && !EnableDesignation && EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - sizefDepartment.Height) / ;
DrawStringOfDepartment(g, sizefDepartment, drawY);
}
else if (!EnableName && EnableDesignation && !EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - sizefDesignation.Height) / ;
DrawStringOfDesignation(g, sizefDesignation, drawY);
}
else if (EnableName && !EnableDesignation && !EnableDepartment && !EnableInstitution)
{
drawY = (picboxPreview.Height - sizefName.Height) / ;
DrawStringOfName(g, sizefName, drawY);
}
}
picboxPreview.SizeMode = PictureBoxSizeMode.Normal;
picboxPreview.Image = bm;
#region DrawString Name|Designation|Department|Institution
private void DrawStringOfName(Graphics g, SizeF sizefName, float drawY)
{
string drawString = txtName.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Bold);
Brush drawBrush = Brushes.Black;
float drawX = (picboxPreview.Width - sizefName.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
private void DrawStringOfDesignation(Graphics g, SizeF sizefDesignation, float drawY)
{
string drawString = txtDesignation.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Regular);
Brush drawBrush = Brushes.Gray;
float drawX = (picboxPreview.Width - sizefDesignation.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
private void DrawStringOfDepartment(Graphics g, SizeF sizefDepartment, float drawY)
{
string drawString = txtDepartment.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Regular);
Brush drawBrush = Brushes.Gray;
float drawX = (picboxPreview.Width - sizefDepartment.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
private void DrawStringOfInstitution(Graphics g, SizeF sizefInstitution, float drawY)
{
string drawString = txtInstitution.Text.Trim();
Font drawFont = new Font("Arial", , FontStyle.Regular);
Brush drawBrush = Brushes.Gray;
float drawX = (picboxPreview.Width - sizefInstitution.Width) / ;
//drawY = (picboxPreview.Height - (sizefName.Height + sizefDesignation.Height + sizefDepartment.Height + sizefInstitution.Height)) / 2;
g.DrawString(drawString, drawFont, drawBrush, drawX, drawY);
}
#endregion
结果:

C# Graphics绘图 picBox的更多相关文章
- 用仿ActionScript的语法来编写html5——第五篇,Graphics绘图
用仿ActionScript的语法来编写html5——第五篇,Graphics绘图 canvas本身就是一个Graphics,可以直接进行绘图在actionscript里面,每个Sprite都有一个G ...
- Graphics绘图闪烁的问题
加入获取的boardPanel.CreateGraphics()时候 用这个g去g.Clear(BackColor);时候会闪烁. 解决办法: 在绘图的时候 用 绘图的image的Graphics. ...
- UIKit和Core Graphics绘图(一)——字符串,线条,矩形,渐变
概述 CoreGraphics也称为Quartz 2D 是UIKit下的主要绘图系统,频繁的用于绘制自定义视图.Core Graphics是高度集成于UIView和其他UIKit部分的.Core Gr ...
- Graphics 绘图
Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力. Graphics类提供基本的几何图形绘制方法,主要有:画线段.画矩形.画圆.画带颜色的图形.画椭圆.画圆弧.画多边形等 ...
- 使用java Graphics 绘图工具生成顺丰快递电子面单
最近公司需要开发一个公司内部使用的快递下单系统,给我的开发任务中有一个生成电子面单功能,为了下单时更方便,利用此功能使用快递公司给我们的打印机直接打印出电子面单,刚接到这个任务时我想这应该很简单,不就 ...
- UIKit和Core Graphics绘图(三)——绘制虚线,椭圆以及饼图
绘制虚线 虚线绘制主要调用CGContextSetLineDash函数. 这个函数有4个参数,除了一个是上下文外,phase为初始跳过几个点开始绘制,第三个参数为一个CGFloat数组,指定你绘制的样 ...
- Core Graphics绘图
首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...
- 1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览
1 新建项目 A 修改pro中的内容如下: HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets prints ...
- iOS绘图系统UIKit与Core Graphics
概述 iOS主要的绘图系统有UIKit,Core Graphics,Core Animation,Core Image,Open GL等,本片博文主要介绍UIKit与Core Graphics的绘图系 ...
随机推荐
- linux bash & profile &bash_profile 小结
login 方式:: su - oracle 依次 /etc/bash.bashrc———— /home/$user/.bashrc ———— /ect/profile ———— /home/$use ...
- 字符串正则替换replace第二个参数是函数的问题
按照JS高程的说法,如下 replace()方法的第二个参数也可以是一个函数.在只有一个匹配项(即与模式匹配的字符串)的情况下,会向这个函数传递3个参数:模式的匹配项.模式匹配项在字符串中的位置和原始 ...
- EF架构~DefaultValue让我的UnitOfWork更可读
回到目录 在编程世界里,使用“否定式”不是一件好事,因为它的可读性并不高,但有时,为了让使用者减少代码量,还是使用了双重否定,直到DefaultValue的出现,它改变了这一切,它可以为你的属性设置你 ...
- 【管理心得之三十】"这事与我无关"
场景再现 ========================事因 ⇔ {一个农庄主在他的粮仓里放了一只老鼠夹.} 过程 ⇔ {老鼠发现了,跑去告诉母鸡} 母鸡:这和我有什么关系,我很同情你. ...
- fir.im Weekly - 2016 开年技术干货分享
开年上班,北上广的技术er 陆续重返"人间".看到别人已返工写代码,竟然有种慌慌的感觉(ง •̀_•́)ง 勤奋好学如你,fir.im weekly 送上最新一波技术分享供你 &q ...
- Java 7 中的Switch 谈 Java版本更新和反编译知识
Java 7 中的Switch 谈 Java版本更新和反编译知识 学习编程,享受生活,大家好,我是追寻梦的飞飞.今天主要讲述的是Java7中的更新Switch实现内部原理和JAD反编 ...
- Java并发包中Semaphore的工作原理、源码分析及使用示例
1. 信号量Semaphore的介绍 我们以一个停车场运作为例来说明信号量的作用.假设停车场只有三个车位,一开始三个车位都是空的.这时如果同时来了三辆车,看门人允许其中它们进入进入,然后放下车拦.以后 ...
- [OpenCV] Samples 02: [ML] kmeans
注意Mat作为kmeans的参数的含义. 扩展:高维向量的聚类. #include "opencv2/highgui.hpp" #include "opencv2/cor ...
- Deep learning:四十三(用Hessian Free方法训练Deep Network)
目前,深度网络(Deep Nets)权值训练的主流方法还是梯度下降法(结合BP算法),当然在此之前可以用无监督的方法(比如说RBM,Autoencoder)来预训练参数的权值,而梯度下降法应用在深度网 ...
- sublime Text3使用笔记
转载:http://blog.csdn.net/u012771929/article/details/30030249 目录: 1.setting 安装插件,package control ,Emme ...