一个JPG文件存入数据库后,调用也正常,但是打印的时候为什么比直接打印原始图片要小呢?
打印代码:
private System.Drawing.Image imga=null;
private void buttonxs_Click(object sender, System.EventArgs e)
{

// string sError;
// string mm=XControls.Global.Request.QueryString("通用查询",sel,out sError);
// Byte mms=Convert.ToByte(mm);
// Byte[] bt=(Byte[])mms; string sServer,sUser,sPwd,sError;
XControls.Global.Request.GetDataServerInfo(out sServer,out sUser,out sPwd,out sError);
string strConnection="uid="+sUser+";pwd="+sPwd+";";
strConnection+="Database=ZXYY;Server="+sServer+";";
SqlConnection conn=new SqlConnection(strConnection); 
SqlCommand cmda=new SqlCommand("select photo from 图片 where ID=1",conn);
conn.Open();   
// SqlDataAdapter sqldt=new SqlDataAdapter(sel,strConnection);
// sqldt.SelectCommand.CommandText=strSle;
SqlDataReader   reader=cmda.ExecuteReader();
reader.Read();   
System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])reader[0]);
// byte[] bt=(byte[])reader[0];
// string  ss=bt.Length.ToString();
System.Drawing.Image img = System.Drawing.Image.FromStream(ms,true);
imga=img;
this.pictureBox1.Visible=true;
this.ax1.Visible=false;
this.pictureBox1.Image=img;
} private void Proof图片_Load(object sender, System.EventArgs e)
{
this.pictureBox1.Visible=false;
}
private System.Drawing.Printing.PrintDocument  docToPrint=new System.Drawing.Printing.PrintDocument();
private void buttonprint_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.PrintDialog print=new PrintDialog();
print.AllowSomePages=true;
print.ShowHelp = true;
print.Document=this. docToPrint;
DialogResult result=print.ShowDialog();
if(result==DialogResult.OK)
{
 docToPrint.Print();
}
}
private void docToPrint_PrintPage(object sender,  System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数
{
// switch(this.streamType)
// {
// case "txt":
// string text = null;
// System.Drawing.Font printFont = new System.Drawing.Font
// ("Arial", 35, System.Drawing.FontStyle.Regular);
//
// // Draw the content.
// System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
// text=streamReader.ReadToEnd();
// e.Graphics.DrawString(text,printFont,System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
// break;
// case "image":
System.Drawing.Image image=imga;
int x=e.MarginBounds.X;
int y=e.MarginBounds.Y;
int width=image.Width;
int height=image.Height;
if((width/e.MarginBounds.Width)>(height/e.MarginBounds.Height))
{
width=e.MarginBounds.Width;
height=image.Height*e.MarginBounds.Width/image.Width;
}
else
{
height=e.MarginBounds.Height;
width=image.Width*e.MarginBounds.Height/image.Height;
}
System.Drawing.Rectangle destRect=new System.Drawing.Rectangle(x,y,width,height);
e.Graphics.DrawImage(image,destRect,0,0,image.Width,image.Height,System.Drawing.GraphicsUnit.Pixel);
// break;
// default:
// break;
// }
   
}
高数帮忙看一下,不知道问题出在那里?

解决方案 »

  1.   

    最后的e.Graphics.DrawImage(image,destRect,0,0,image.Width,image.Height,System.Drawing.GraphicsUnit.Pixel); 
    好像有点问题
      

  2.   

    e.Graphics.DrawImage(image,destRect,0,0,width,height,System.Drawing.GraphicsUnit.Pixel); 
    才对好像
      

  3.   

    还是不行,打出来和在winform上picturebox显示的一样大了
    但是我不是用picturebox的image打印的
    System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])reader[0]);
    System.Drawing.Image image=imga;
    System.Drawing.Image img = System.Drawing.Image.FromStream(ms,true);
    imga=img;调用的是数据库取出来的数据流。
    这真郁闷了
      

  4.   

    应该不会出现这种问题,就算是本地文件,也是通过Imgae.FromFile(好像是个这个方法)获取到一个Image,实际的加载过程仍然是Stream你先调试下,看能否获取到从数据库中得到的imga的width和height