关于水晶报表动态设置图片,同比例缩放问题

string path = "";
                byte[] bytes;
                try
                {
                    path = Server.MapPath( "..//img//logoCompany.gif" );
                    FileStream f = new FileStream( path, FileMode.Open, FileAccess.Read );
                    BinaryReader br = new BinaryReader( f );
                    //通过BinaryReader把图片流读成二进制数据
                    bytes = br.ReadBytes( (int) f.Length );
                }
                catch
                {
                    bytes = null;
                }
                Bitmap img = null;
                try
                {
                    img = new Bitmap( new MemoryStream( bytes ) );
                } //转化为图片,控制像素Width和Heigth
                catch
                { }
                for ( int i = 0; i < dt.Rows.Count; i++ )
                {
                    //为了防止翻页刷新掉Logo把所记录的新增列都更新成Logo的二进制数据
                    dt.Rows[i]["Image"] = bytes;
                }
                string repoarpath = Server.MapPath("CR_APDetail.rpt");
                myReport.Load(repoarpath, OpenReportMethod.OpenReportByTempCopy);
                myReport.SetDataSource(ds);

                //----------------------------
                try
                {
                    //这里是增加对水晶报表里图片控件的引用,ReportObjects["Image1"] 中的Image1就是我们拉到水晶报表里的图片字段的 ID编码
                    BlobFieldObject x = (BlobFieldObject) myReport.ReportDefinition.ReportObjects["Image1"];
                    EditImage( ref img, ref x );//同比例缩放图片函数
                }
                catch
                {
                }

                CrystalReportViewer1.DisplayGroupTree = false;
                CrystalReportViewer1.ReportSource = myReport;

 

关于同比例缩放函数,很简单,首先获取水晶报表Image控件的宽和高,然后根据显示的图片大小进行同比例放大或缩小即可。

如有错误,欢迎指正!

你可能感兴趣的:(关于水晶报表动态设置图片,同比例缩放问题)