关于图像绘制并输出的问题,大家帮忙看看
2011-03-02 21:02
459 查看
我现在在做个绘图软件,恩,就是那种读取主板文件,并绘制电路元件图的那种软件,我现在要实现的就是打印这个功能。
我不太会,老是会出问题。
代码:
/*********文件读取**********/
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 画圆
请大家帮忙看看!谢谢!
我不太会,老是会出问题。
代码:
/*********文件读取**********/
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 画圆
请大家帮忙看看!谢谢!
相关文章推荐
- HTML5 绘制图像(上)之:关于canvas元素引领下一代web页面的问题
- achartengine之折线图---简单用法(续二)---(动态刷新)图形嵌套在页面(有问题请大家帮忙看看),已解决问题
- 新手问题,大家帮忙看看..谁有意见散分给谁哦!
- [原创] 关于Type Initializer和 BeforeFieldInit的问题,看看大家能否给出正确的解释
- 关于链表的,有一些错误老是解决不了,大家帮忙看看
- 关于web,我有一个尴尬的问题想请大家帮忙!
- 一个ASP.NET2.0的问题,实在无法解决,请大家帮忙看看(放在首页一天后撤下,请DUDU原谅)问题已经解~~^_^
- 关于openkey的问题,请大家帮看看!
- 网站seo 的问题大家帮忙看看是什么原因。
- 代码问题,大家来帮忙看看啊
- 关于用setMaskFilter设置绘制图像发光失效问题
- 最近在探讨关于缓冲区溢出的相关问题,各位大侠帮忙看看这个程序,两个for循环导致内存内容不一样,其中的玄妙在哪里?我是嫩草,谢啦谢啦~~
- 最近遇到一个非常棘手的需求是关于邮件转发的问题,请大家来帮忙给出出主意如何实现。
- 关于系统首页绘制问题(ext布局+c#后台添加数据)经html输出流输出响应客户端
- 关于Struts问题,大家帮忙啊
- 关于xslt的问题,请大家帮忙看下
- js调用asp时出现的郁闷问题,大家帮忙看看
- 关于系统首页绘制问题(ext布局+c#后台加入数据)经html输出流输出响应client
- php使用GD图像库绘制输出图像出现乱码问题和图片上输出中文出现乱码问题解决方法。
- 遇到的一个关于NULL小问题,请大家帮忙看下,求个解释