我现在在做个绘图软件,恩,就是那种读取主板文件,并绘制电路元件图的那种软件,我现在要实现的就是打印这个功能。
我不太会,老是会出问题。
代码:
/*********文件读取**********/
private string[] allfile;//定义放多个文件的数组
ClassBDR bdr = new ClassBDR();
float R = 1.0f;//定义圆的半径
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dlgOpenFile = new OpenFileDialog();
dlgOpenFile.Filter = "BDR文件|*.BDR";
dlgOpenFile.Title = "打开文件";
dlgOpenFile.Multiselect = true;//选取多个档案
try
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
allfile = dlgOpenFile.FileNames;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Pen pen2 = new Pen(Color.Blue, 0.01f);
Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
//改变坐标系统的原点,把坐标原点设置在pictrue画面的中心点
g.TranslateTransform((float)(pictureBox1.Width / 2 / 96), (float)(pictureBox1.Height / 2 / 96));
g.PageUnit = GraphicsUnit.Inch;//指定英尺作为测量单位
g.PageScale = 1; //指定绘图单位和书面单位之间的缩放
for (int i = 0; i < allfile.Length; i++)
{
//把文件中的所有行读入数组
string[] listfield = File.ReadAllLines(allfile[i]);
//定义一个数组用来存储坐标点
string[] getXY;
//划分数组
if (listfield == null)
return;
else //if(dlgOpenFile.Filter=="*.BDR")
{
for (int j = 0; j < listfield.Length; j++)
{
if (listfield[j].Substring(0, 1) == "T")
continue;
else if (listfield[j].Substring(0, 1) == "M")
continue;
else
{
getXY = listfield[j].Split('Y');
bdr.PointX = getXY[0].Substring(1);
bdr.PointY = getXY[1];
//画图
g.DrawEllipse(pen2, Convert.ToSingle(bdr.PointX) / 10000.0f, Convert.ToSingle(bdr.PointY) / 10000.0f, (float)(R / 25.4f), (float)(R / 25.4f));
}
}
}
}
//释放资源
g.Dispose();
}
/*********执行**********/
//打印
private void btnPrint_Click(object sender, EventArgs e)
{
}
//打印预览
private void btnPp_Click(object sender, EventArgs e)
{
this.dlgPp.Document = Pd;
this.dlgPp.ShowDialog();
}
//打印设置
private void btnPs_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//初始化画图界面
FormatPbox();
}
public void FormatPbox()//初始化画图界面
{
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Pen pen1 = new Pen(Color.Red, (float)0.5);
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.DrawLine(pen1, 0, pictureBox1.Height / 2, pictureBox1.Width, pictureBox1.Height / 2);
g.DrawLine(pen1, pictureBox1.Width / 2, 0, pictureBox1.Width / 2, pictureBox1.Height);
g.Dispose();
}
主要问题出现在下面这段代码中的红色字体部分:
private Image images;
private void Pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
images = this.pictureBox1.Image;
//获取打印机纸张的宽度和高度
int printWidth = Pd.DefaultPageSettings.PaperSize.Width;
int printHeight = Pd.DefaultPageSettings.PaperSize.Height;
//判断图片的宽度是否小于等于纸张的宽度
if (Convert.ToUInt32(pictureBox1.Image.Width) <= printWidth)
{
//如果图片的宽度小于等于纸张的宽度,则让图片处在纸张的中间
float x = (printWidth - Convert.ToUInt32(pictureBox1.Image.Width)) / 2;
float y = (printHeight - Convert.ToUInt32(pictureBox1.Image.Height)) / 2;
//使用DrawImage方法重新绘制图片
e.Graphics.DrawImage(Image.FromFile(images), x, y, Convert.ToUInt32(pictureBox1.Image.Width), Convert.ToUInt32(pictureBox1.Image.Height));
}
else
{
//判断图片的宽度是否大于纸张的宽度
if (Convert.ToUInt32(pictureBox1.Image.Width) > printWidth)
{
//宽度大于高度则让图片旋转90度垂直显示
Bitmap bitmap = (Bitmap)Bitmap.FromFile(images);
bitmap.RotateFlip(RotateFlipType.Rotate90FlipXY);
//实例化对象
PictureBox pb = new PictureBox();
//设置Image属性
pb.Image = bitmap;
//计算图片高度占纸张宽度的比例
Single a = printWidth / Convert.ToSingle(pictureBox1.Image.Height);
//重新绘制图片
e.Graphics.DrawImage(pb.Image, 0, 0, Convert.ToSingle(pictureBox1.Image.Height) * a, Convert.ToSingle(pictureBox1.Image.Width) * a);
}
else
{
//计算图片高度占纸张宽度的比例
Single a = printWidth / Convert.ToSingle(pictureBox1.Image.Width);
//重新绘制
e.Graphics.DrawImage(Image.FromFile(images), 0, 0, Convert.ToSingle(pictureBox1.Image.Width) * a, Convert.ToSingle(pictureBox1.Image.Height) * a);
}
}
}
错误提示:
錯誤 1 最符合的多載方法 'System.Drawing.Image.FromFile(string)' 有一些無效的引數 E:/个人信息/画圆/画圆/Form1.cs 184 38 画圆
錯誤 2 引數 '1': 無法從 'System.Drawing.Image' 轉換為 'string' E:/个人信息/画圆/画圆/Form1.cs 184 53 画圆
錯誤 3 最符合的多載方法 'System.Drawing.Image.FromFile(string)' 有一些無效的引數 E:/个人信息/画圆/画圆/Form1.cs 192 45 画圆
錯誤 4 引數 '1': 無法從 'System.Drawing.Image' 轉換為 'string' E:/个人信息/画圆/画圆/Form1.cs 192 61 画圆
錯誤 5 最符合的多載方法 'System.Drawing.Image.FromFile(string)' 有一些無效的引數 E:/个人信息/画圆/画圆/Form1.cs 208 42 画圆
錯誤 6 引數 '1': 無法從 'System.Drawing.Image' 轉換為 'string' E:/个人信息/画圆/画圆/Form1.cs 208 57 画圆
请大家帮忙看看!谢谢!