实例介绍
【实例简介】
根据数据库查询BOM,查询子项物料数量,汇总后给自动给实际图纸添加数量水印,便于生产计划操作
【实例截图】
【核心代码】
//数据库连接获取数量
DataTable table = new DataTable();
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "server=.;database=AIS20200312125333;uid=zz;pwd=zz";
sqlcon.Open();
SqlCommand com = new SqlCommand();
com.Connection = sqlcon;
com.CommandType = CommandType.Text;
com.CommandText = "";
//SqlDataReader dr = com.ExecuteReader();//执行SQL语句
//int KKone = Convert.ToInt32(com.ExecuteScalar());
//dr.Close();//关闭执行
//MessageBox.Show(Convert.ToString(1));
sqlcon.Close();//关闭数据库
#region
StringBuilder sb = new StringBuilder();
List<jh> jhs = new List<jh>();
jh csjh = new jh();
csjh.jhbm = textBox4.Text.Trim();
csjh.jhcj = "0";
csjh.jhsl = "1";
csjh.jhzx = "1";
jhs.Add(csjh);
for (int y = 0; y < 10; y )
{
List<jh> jhtext = new List<jh>();
foreach (jh jhobj in jhs)
{
jhtext.Add(jhobj);
}
foreach (jh jhobj in jhtext)
{
if (jhobj.jhcj == Convert.ToString(y) && jhobj.jhzx == "1")
{
sb.AppendFormat(@"/*dialect*/
SELECT * FROM T_ENG_BOM where FMATERIALID in
(select FMATERIALID from T_BD_MATERIAL where FNUMBER='{0}') and FDocumentStatus='c'
order by FCREATEDATE desc
", jhobj.jhbm);
sqlcon.Open();//开启数据库
table.Clear();
com.CommandText = sb.ToString();
sb.Remove(0, sb.Length);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(com);
sqlAdapter.Fill(table);
sqlcon.Close();//关闭数据库
if (table.Rows.Count == 0)
continue;
//水印
private void AddFontWatermark(string txt,string Iname,int i)
{
b = new SolidBrush(fontColor); //创建SolidBrush对象
bt = new Bitmap(368,75); //创建Bitmap对象,设置水印图片大小
BigBt = new Bitmap(Image.FromFile(ImgDirectoryPath "\\" Iname)); //通过图片路径创建Bitmap对象
Graphics g = Graphics.FromImage(bt); //创建Graphics对象
Graphics g1 = Graphics.FromImage(BigBt); //创建Graphics对象
g.Clear(Color.Gainsboro); //设置画布背景颜色
pbImgPreview.Image = bt; //设置Image属性
if (FontF == null) //判断是否设置字体
{
f = new Font(txt,fontSize); //创建字体对象
SizeF XMaxSize = g.MeasureString(txt, f); //获取字体的大小
Fwidth = (int)XMaxSize.Width; //字体宽度
Fheight = (int)XMaxSize.Height; //字体高度
g.DrawString(txt,f, b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2); //绘制文字水印
if (cbbPosition.SelectedIndex==0) //正中
{
g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2);
}
if (cbbPosition.SelectedIndex == 1) //左上
{
g1.DrawString(txt, f, b, 30,30);
}
if (cbbPosition.SelectedIndex ==2) //左下
{
g1.DrawString(txt, f, b, 30, (int)(BigBt.Height - Fheight)-30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), 30);
}
if (cbbPosition.SelectedIndex == 4) //右下
{
g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);
}
}
else //如果设置了字体
{
f = new Font(FontF, fontSize, fontStyle); //创建字体对象
SizeF XMaxSize = g.MeasureString(txt, f); //获取字体的大小
Fwidth = (int)XMaxSize.Width; //字体宽度
Fheight = (int)XMaxSize.Height; //字体高度
//绘制文字水印
g.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2);
if (cbbPosition.SelectedIndex == 0) //正中
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth) / 2,
(int)(BigBt.Height - Fheight) / 2);
}
if (cbbPosition.SelectedIndex == 1) //左上
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, 30);
}
if (cbbPosition.SelectedIndex == 2) //左下
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, (int)(BigBt.Height - Fheight)-30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), Fheight);
}
if (cbbPosition.SelectedIndex == 4) //右下
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b,
(int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);
}
}
if (i == 1) //i为1时,保存绘制了文字谁赢的图片
{
string ipath; //加水印后图片路径
if (NewFolderPath.Length == 3) //判断是不是系统磁盘
//如果是系统磁盘则删除“:”后边的字符
ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") 1);
else //否则
ipath = NewFolderPath; //获取路径
//获取图片类型
string imgstype = Iname.Substring(Iname.LastIndexOf(".") 1, Iname.Length - 1 - Iname.LastIndexOf("."));
if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg") //如果原图是jpeg格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Jpeg); //加水印后依然保存成jpeg
}
if (imgstype.ToLower() == "png") //如果原图是png格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Png); //加水印后依然保存成png格式
}
if (imgstype.ToLower() == "bmp") //如果原图是bmp格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Bmp); //加水印后依然保存成bmp
}
if (imgstype.ToLower() == "gif") //如果原图是gif格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Gif); //加水印后依然保存成gif
}
g1.Dispose();
BigBt.Dispose();
}
if (i == 2) //加完图片水印后预览
{
if (cbbPosition.SelectedIndex == 0) //正中
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);
}
if (cbbPosition.SelectedIndex == 1) //左上
{
g1.DrawImage(effect, 30, 30);
}
if (cbbPosition.SelectedIndex == 2) //左下
{
g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, 30);
}
if (cbbPosition.SelectedIndex == 4) //右下
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, (int)(BigBt.Height - effect.Height) - 30);
}
}
if (i == 3) //加完图片水印后保存
{
if (cbbPosition.SelectedIndex == 0) //正中
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);
}
if (cbbPosition.SelectedIndex == 1) //左上
{
g1.DrawImage(effect, 30, 30);
}
if (cbbPosition.SelectedIndex == 2) //左下
{
g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), 30);
}
if (cbbPosition.SelectedIndex == 4) //右下
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), (int)(BigBt.Height - effect.Height) - 30);
}
string ipath;
if (NewFolderPath.Length == 3)
ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") 1);
else
ipath = NewFolderPath;
string imgstype = Iname.Substring(Iname.LastIndexOf(".") 1, Iname.Length - 1 - Iname.LastIndexOf("."));
if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg") //如果原图是jpeg格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Jpeg); //加水印后依然保存成jpeg格式
}
if (imgstype.ToLower() == "png") //如果原图是png格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Png); //加水印后依然保存成png格式
}
if (imgstype.ToLower() == "bmp") //如果原图是bmp格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Bmp); //加水印后依然保存成bmp格式
}
if (imgstype.ToLower() == "gif") //如果原图是gif格式
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Gif); //加水印后依然保存成gif格式
}
}
}
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论