这个事生成一个图片的例子。
private void ShowReportImgs(int types, string pathId, int reportTypes)
{
#region 获取数据
Hashtable detailFirst = new Hashtable();// getPathDetail(pathId, "1001");//明细信息
Hashtable detailConfirm = new Hashtable();//getPathDetail(pathId, "1002");//明细信息
Hashtable path = getPath(pathId);//信息
if (path.Count == 0) return;
string url1 = "";
string url2 = "";
string url3 = "";
string url4 = "";
string remark1 = "";
string remark2 = "";
string remark3 = "";
string remark4 = "";
string reportContents = "";
string closeReporttime = "";
string chuzhenEmpAccID = "";
string fuzhenEMpAccID = "";
try
{
switch (reportTypes)
{
........
}
}
catch (Exception)
{
return;
}
#endregion
//===============================================================================以上是取数据模块,下面是计算这些数据所要用的空间(高度,宽度是固定的)
#region 计算高度
int heiPaing = 10;//行与行之间的间隔距离
int heig = 40;
int manping = 65;//左右两边的空格距离
int MaxWidth = 795;
int MaxHeight = 1224;
Bitmap MyMap = new Bitmap(MaxWidth, MaxHeight);
Graphics MyG = Graphics.FromImage(MyMap);
#region
int jjHeighttemp = 210;//开始y坐标
// heig = 260;
List<string> tempsdtemp = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), path["Generally"].ToString().Trim());
int lgstemp = tempsdtemp.Count;
for (int i = 0; i < lgstemp; i++)
{
jjHeighttemp += 25;
}
#endregion
#region 图像
//换行,图片340
int imgsHeighttemp = jjHeighttemp > 340 ? jjHeighttemp + 20 : 340;//指定图像模块开始y坐标
//创建区域
imgsHeighttemp = imgsHeighttemp + 25;
int remYtoptemp = 0;
try
{
// Rectangle rct = new Rectangle(imgOdd, imgsHeight, imgW, imgH);
int tempRemYtoptemp = 0; remYtoptemp = 0;
if (!string.IsNullOrEmpty(remark1))
{
remYtoptemp = imgsHeighttemp + imgH + 10;
List<string> reList1temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark1);
int lgs = reList1temp.Count;
for (int i = 0; i < lgs; i++) { remYtoptemp = remYtoptemp + 20; }
}
if (!string.IsNullOrEmpty(remark2))
{
remYtoptemp = imgsHeighttemp + imgH + 10;
List<string> reList2temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark2);
int lgs = reList2temp.Count;
for (int i = 0; i < lgs; i++) { remYtoptemp = remYtoptemp + 20; }
tempRemYtoptemp = remYtoptemp;
}
remYtoptemp = Math.Max(remYtoptemp, tempRemYtoptemp);
if (remYtoptemp == 0) imgsHeighttemp += remYtoptemp + 10;
else imgsHeighttemp = remYtoptemp + 10;
tempRemYtoptemp = 0; remYtoptemp = 0;
if (!string.IsNullOrEmpty(remark3))
{
remYtoptemp = imgsHeighttemp + imgH + 10;
List<string> reList3temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark3);
int lgs = reList3temp.Count;
for (int i = 0; i < lgs; i++) { remYtoptemp = remYtoptemp + 20; }
}
if (!string.IsNullOrEmpty(remark4))
{
tempRemYtoptemp = imgsHeighttemp + imgH + 10;
List<string> reList4temp = GetStringLine(MyG, new Font("宋体", 12), imgW, remark4);
int lgs = reList4temp.Count;
for (int i = 0; i < lgs; i++) { tempRemYtoptemp = tempRemYtoptemp + 20; }
}
remYtoptemp = Math.Max(remYtoptemp, tempRemYtoptemp);//取得备注1和备注2坐标的大者
if (remYtoptemp == 0) imgsHeighttemp += remYtoptemp + 10;
else imgsHeighttemp = remYtoptemp + 10;
}
catch
{
}
#endregion
#region 诊断
int pathHeighttemp = imgsHeighttemp;// > 870 ? imgsHeighttemp + 20 : 870;//块开始y坐标
List<string> conListtemp = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), reportContents);
int lgse = conListtemp.Count;
for (int i = 0; i < lgse; i++) { pathHeighttemp += 25; }
#endregion
#region 页尾部分
//换行划线
int tailHeighttemp = pathHeighttemp > 1050 ? pathHeighttemp + 160 : 1050 + 160;//尾部开始y坐标
#endregion
//=========================计算完成,下面是开始画图
#endregion
MyMap.Dispose();
#region 页头部分
MyMap = new Bitmap(MaxWidth, tailHeighttemp);
MyG.Clear(Color.White);
MyG.Dispose();
MyG = Graphics.FromImage(MyMap);
MyG.Clear(Color.White);
string textInf = getHostMsg("LocalConsultation");
textInf = textInf + "";
try
{
//30px
MyG.DrawString(textInf, new Font("宋体", 22, FontStyle.Bold), Brushes.Black, new Point(((MaxWidth - textInf.Length * 30) / 2), heig), StringFormat.GenericTypographic);
}
catch (Exception) { }
heig += heiPaing + 30;//行间隔+字体高度
//加载文字 25px
string text2 = "报告";
MyG.DrawString(text2, new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(((MaxWidth - text2.Length * 25) / 2), heig), StringFormat.GenericTypographic);
heig += heiPaing + 10;//行间隔+字体高度
//加载文字 25px
string stringsef = "号码:" + path["ObjID"].ToString().Trim();
MyG.DrawString(stringsef, new Font("宋体", (float)10.5, FontStyle.Bold), Brushes.Black, new Point(((MaxWidth - stringsef.Length * 20 - 20)), heig), StringFormat.GenericTypographic);
#endregion
#region 信息
int basicHeight = 110;//基本信息开始y坐标
//换行划横线
basicHeight += heiPaing;
MyG.DrawLine(new Pen(Color.Black, 2.4f), new Point(manping, basicHeight), new Point(MaxWidth - manping, basicHeight));
//换行:
basicHeight += heiPaing + 10;
MyG.DrawString("姓名:", new Font("宋体", 12), Brushes.Black, new Point(60, basicHeight));
int lgode = 60 + 5 * 16;
MyG.DrawString(path["Name"].ToString().Trim(), new Font("宋体", 12), Brushes.Black, new Point(lgode, basicHeight));
int mmlg = ((MaxWidth - manping * 2) / 3);
MyG.DrawString("性别:", new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60, basicHeight));
string sex = path["Sex"].ToString().Trim();
MyG.DrawString(sex, new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60 + 5 * 16, basicHeight));
string age = path["Age"].ToString().Trim();
MyG.DrawString("年龄:", new Font("宋体", 12), Brushes.Black, new Point(mmlg * 2 + age.Length * 15 + 20, basicHeight));
MyG.DrawString(age, new Font("宋体", 12), Brushes.Black, new Point(mmlg * 2 + 60 + age.Length * 5 + 16 * 3 + 20, basicHeight));
//换行:
basicHeight += heiPaing + 20;
MyG.DrawString("生:", new Font("宋体", 12), Brushes.Black, new Point(60, basicHeight));
lgode = 60 + 5 * 16;
string sjdoctor = getEmp(path["UploadAccSysID"].ToString().Trim())[0];
MyG.DrawString(sjdoctor, new Font("宋体", 12), Brushes.Black, new Point(lgode, basicHeight));
mmlg = ((MaxWidth - manping * 2) / 3);
MyG.DrawString("日期:", new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60, basicHeight));
string sjdate = DateTime.Parse(path["CreateTime"].ToString().Trim()).ToShortDateString();
MyG.DrawString(sjdate, new Font("宋体", 12), Brushes.Black, new Point(mmlg + 60 + 5 * 16, basicHeight));
//换行划线
basicHeight += heiPaing + 16;
MyG.DrawLine(new Pen(Color.Black, 2.4f), new Point(60, basicHeight), new Point(MaxWidth - 60, basicHeight));
#endregion
#region
int jjHeight = 210;//开始y坐标
MyG.DrawString("检查:", new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(60, jjHeight), StringFormat.GenericTypographic);
List<string> tempsd = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), path["Generally"].ToString().Trim());
int lg2s = tempsd.Count;
for (int i = 0; i < lg2s; i++)
{
jjHeight += 25;
MyG.DrawString(tempsd[i], new Font("宋体", 12), Brushes.Black, new Point(60, jjHeight));
}
#endregion
#region 图像
//换行,图片340
int imgsHeight = jjHeight > 340 ? jjHeight + 20 : 340;//模块开始y坐标
MyG.DrawString("图像:", new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(60, imgsHeight));
//创建图片路径
//创建区域
imgsHeight = imgsHeight + 25;
int imgOdd = 141;//(MaxWidth - manping * 2) / 4 + manping - img1.Width / 2; //x1坐标
int imgEven = 410;// (MaxWidth - manping * 2) / 4 * 3 + manping - img1.Width / 2; //x2坐标
int remYtop = 0;
try
{
int wi, hi;
System.Drawing.Image img1;
System.Drawing.Image img2;
System.Drawing.Image img3;
System.Drawing.Image img4;
// Rectangle rct = new Rectangle(imgOdd, imgsHeight, imgW, imgH);
int tempRemYtop = 0;
if (!string.IsNullOrEmpty(url1))
{
wi = 0; hi = 0;
// url1 = FileHostUrl + "/" + url1;
try
{
MemoryStream ms1 = new MemoryStream(getBytes(url1, null, null, ""));
img1 = System.Drawing.Image.FromStream(ms1);
getImageWithAndHeigth(img1, out wi, out hi, imgW, imgH);
Rectangle rct = new Rectangle(imgOdd, imgsHeight, wi, hi);
MyG.DrawImage(img1, rct);//画图1
img1.Dispose();
ms1.Close();
ms1.Dispose();
}
catch (Exception ex)
{
}
remYtop = imgsHeight + hi + 10;
List<string> reList1 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark1);
for (int i = 0; i < reList1.Count; i++)
{
MyG.DrawString(reList1[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgOdd, remYtop));
remYtop = remYtop + 20;
}
}
if (!string.IsNullOrEmpty(url2))
{
wi = 0; hi = 0;
// url2 = FileHostUrl + "/" + url2;
try
{
MemoryStream ms2 = new MemoryStream(getBytes(url2, null, null, ""));
img2 = System.Drawing.Image.FromStream(ms2);
getImageWithAndHeigth(img2, out wi, out hi, imgW, imgH);
Rectangle rct = new Rectangle(imgEven, imgsHeight, wi, hi);
MyG.DrawImage(img2, rct);//画图1
img2.Dispose();
ms2.Close();
ms2.Dispose();
}
catch (Exception)
{ }
tempRemYtop = imgsHeight + hi + 10;
List<string> reList2 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark2);
for (int i = 0; i < reList2.Count; i++)
{
MyG.DrawString(reList2[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgEven, tempRemYtop));
tempRemYtop = tempRemYtop + 20;
}
}
remYtop = Math.Max(remYtop, tempRemYtop);//取得备注1和备注2坐标的大者
if (remYtop == 0) imgsHeight += remYtop + 20;
else imgsHeight = remYtop + 20;
remYtop = 0;
tempRemYtop = 0;
if (!string.IsNullOrEmpty(url3))
{
wi = 0; hi = 0;
//url3 = FileHostUrl + "/" + url3;
try
{
MemoryStream ms3 = new MemoryStream(getBytes(url3, null, null, ""));
img3 = System.Drawing.Image.FromStream(ms3);
getImageWithAndHeigth(img3, out wi, out hi, imgW, imgH);
Rectangle rct = new Rectangle(imgOdd, imgsHeight, wi, hi);
MyG.DrawImage(img3, rct);//画图1
img3.Dispose();
ms3.Close();
ms3.Dispose();
}
catch (Exception)
{
}
remYtop = imgsHeight + hi + 10;
List<string> reList3 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark3);
for (int i = 0; i < reList3.Count; i++)
{
MyG.DrawString(reList3[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgOdd, remYtop));
remYtop = remYtop + 20;
}
}
if (!string.IsNullOrEmpty(url4))
{
wi = 0; hi = 0;
// url4 = FileHostUrl + "/" + url4;
try
{
MemoryStream ms4 = new MemoryStream(getBytes(url4, null, null, ""));
img4 = System.Drawing.Image.FromStream(ms4);
getImageWithAndHeigth(img4, out wi, out hi, imgW, imgH);
Rectangle rct = new Rectangle(imgEven, imgsHeight, wi, hi);
MyG.DrawImage(img4, rct);//画图1
img4.Dispose();
ms4.Close();
ms4.Dispose();
}
catch (Exception)
{
}
tempRemYtop = imgsHeight + hi + 10;
List<string> reList4 = GetStringLine(MyG, new Font("宋体", 12), imgW, remark4);
for (int i = 0; i < reList4.Count; i++)
{
MyG.DrawString(reList4[i], new Font("宋体", 9, FontStyle.Bold), Brushes.Black, new Point(imgEven, tempRemYtop));
tempRemYtop = tempRemYtop + 20;
}
}
remYtop = Math.Max(remYtop, tempRemYtop);//取得备注1和备注2坐标的大者
if (remYtop == 0) imgsHeight += remYtop + 20;
else imgsHeight = remYtop + 20;
}
catch
{
}
#endregion
#region
int pathHeight = imgsHeight;// > 870 ? imgsHeight + 20 : 870;//开始y坐标
MyG.DrawString("测试:", new Font("宋体", 12, FontStyle.Bold), Brushes.Black, new Point(60, pathHeight));
List<string> conList = GetStringLine(MyG, new Font("宋体", 12), (MaxWidth - 120), reportContents);
int reportLine = conList.Count;
for (int i = 0; i < reportLine; i++) { pathHeight += 25; MyG.DrawString(conList[i], new Font("宋体", 12), Brushes.Black, new Point(60, pathHeight)); }
#endregion
#region 页尾部分
//以下是页尾部分
//换行划线
int tailHeight = pathHeight > 1050 ? pathHeight + 20 : 1050;//尾部开始y坐标
tailHeight += heiPaing + 16;
MyG.DrawLine(new Pen(Color.Black, 2.4f), new Point(60, tailHeight), new Point(MaxWidth - 60, tailHeight));
//换行,日期
tailHeight += heiPaing + 10;
MyG.DrawString("日期:", new Font("宋体", 12), Brushes.Black, new Point(60, tailHeight));
lgode = 60 + 5 * 16;
string conDate = DateTime.Parse(closeReporttime).ToShortDateString();
MyG.DrawString(conDate, new Font("宋体", 12), Brushes.Black, new Point(lgode, tailHeight));
//换行,日期
tailHeight += heiPaing + 15;
MyG.DrawString("第一个人:", new Font("宋体", 12), Brushes.Black, new Point(60, tailHeight));
string userName = "";
string[] sdoc = new string[2];//
if (!string.IsNullOrEmpty(chuzhenEmpAccID.Trim()))
{
sdoc = getEmp(chuzhenEmpAccID);//
lgode = 60 + 5 * 16;
userName = sdoc[0] != "" ? sdoc[0] : "";
MyG.DrawString(userName, new Font("宋体", 12), Brushes.Black, new Point(lgode, tailHeight));
}
string[] edoc = new string[2];//第二个人的数据
int temp = (MaxWidth - manping * 2) / 2 + manping;
MyG.DrawString("二个人:", new Font("宋体", 12), Brushes.Black, new Point(temp, tailHeight));
if (!string.IsNullOrEmpty(fuzhenEMpAccID.Trim()))
{
edoc = getEmp(fuzhenEMpAccID);
userName = edoc[0] != "" ? edoc[0] : "";
MyG.DrawString(edoc[0], new Font("宋体", 12), Brushes.Black, new Point(temp + 76, tailHeight));
}
tailHeight += heiPaing + 15;
MyG.DrawString("签字:", new Font("宋体", 12), Brushes.Black, new Point(60, tailHeight));//
try
{
if (!string.IsNullOrEmpty(sdoc[1]))
{
MemoryStream ms = new MemoryStream(getBytes(sdoc[1], null, null, ""));
System.Drawing.Image sign = System.Drawing.Image.FromStream(ms);
Rectangle rct = new Rectangle(110, tailHeight, 100, 30);
MyG.DrawImage(sign, rct);//数字签名
sign.Dispose();
ms.Close();
ms.Dispose();
}
}
catch
{
}
MyG.DrawString("签字:", new Font("宋体", 12), Brushes.Black, new Point(temp, tailHeight));
try
{
if (!string.IsNullOrEmpty(edoc[1]))
{
MemoryStream ms = new MemoryStream(getBytes(edoc[1], null, null, ""));
System.Drawing.Image sign = System.Drawing.Image.FromStream(ms);
Rectangle rct = new Rectangle(temp + 50, tailHeight, 100, 30);
MyG.DrawImage(sign, rct);//数字签名
sign.Dispose();
ms.Close();
ms.Dispose();
}
}
catch
{
}
#endregion
if (types == 1)
{
//输出显示
MyMap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
if (types == 2)
{
MemoryStream ms = new MemoryStream();
MyMap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("report.jpg", System.Text.Encoding.UTF8));
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
}
MyMap.Dispose();
MyG.Dispose();
}
其他方法:文本换行用。
public static List<string> GetStringLine(Graphics dc, Font font, int nWidth, string str)
{
int nStrCount = str.Length;
List<string> strLine = new List<string>();
int nStartPos = 0;
int nEndPos = 0;
for (int nIndex = 0; nIndex < nStrCount; nIndex++)
{
nEndPos = nIndex;
if (dc.MeasureString(str.Substring(nStartPos, nEndPos - nStartPos + 1), font).Width > nWidth)
{
strLine.Add(str.Substring(nStartPos, nEndPos - nStartPos));
nStartPos = nEndPos;
}
if (nIndex == nStrCount - 1)
{
strLine.Add(str.Substring(nStartPos));
}
}
return strLine;
}
以上代码分为三部分,
第一部分是先取出数据,对数据进行处理,第二部分是对数据进行预加载,看看这个画板要多大。第三部分是开始填充画板。最后是显示或者下载。