如何在水晶报表中动态显示任意格式的图片?

       前段时间,Accounting 项目内需要在水晶报表中动态显示任意格式的图片。但是,水晶报表只支持OleDb的静态加载图片和动态在 IFieldObject中显示图片,显然,静态加载是不行的了,而后者仅支持JPEG和BMP格式的图片。因此,我们需要把任意格式的图片预先转换成受支持的JEPG或BMP格式,再填充到DataSet中,然后动态地推入到水晶报表中。

      下面的示例中将讲述如何将一个给定路径或从其它途径得到的一个图片流转换成JPEG格式并返回成一个字节数组,以提供给后续应用:

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Imaging;
None.gif
using  System.IO;
None.gif
None.gif
namespace  Common
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 与图像相关的工具类。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ImageUtil
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ImageUtil()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从指定路径中读取一个图像文件并保存到字节数组中。
InBlock.gif        
/// 此方法供水晶报表显示图片使用,所返回字节数组是 BMP 或 JEPG 格式图像数据的数组。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="path">指定的文件路径</param>
ExpandedSubBlockEnd.gif        
/// <returns>从图像中读取出的数据。</returns>

InBlock.gif        public static byte[] ReadImage(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FileStream stream 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                stream 
= File.OpenRead(path);
InBlock.gif                
return ReadImage(stream);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(stream != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    stream.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从给定的流中读取数据到一个字节数组中,并返回此数组。
InBlock.gif        
/// 如果给定的流不是一个图像格式的流,将报异常。
InBlock.gif        
/// 返回的字节数组中,将非BMP和JEPG格式的图像数据流转换为JEPG格式输出,以支持大多数应用。
InBlock.gif        
/// 适用于直接从数据库中读取的二进制图像流的处理。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="stream">给定的图像数据流。</param>
ExpandedSubBlockEnd.gif        
/// <returns>从流中读取的数据。</returns>

InBlock.gif        public static byte[] ReadImage(Stream stream)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Image image 
= Image.FromStream(stream);
InBlock.gif            
byte[] myImage = null;
InBlock.gif
InBlock.gif            
if(image.RawFormat.Guid != ImageFormat.Jpeg.Guid && image.RawFormat.Guid != ImageFormat.Bmp.Guid)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MemoryStream memStream 
= new MemoryStream();
InBlock.gif                image.Save(memStream, ImageFormat.Jpeg);
InBlock.gif                myImage 
= memStream.GetBuffer();
InBlock.gif                memStream.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                stream.Position 
= 0;
InBlock.gif                myImage 
= new byte[stream.Length];
InBlock.gif                stream.Read(myImage, 
0, (int)stream.Length);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return myImage;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

下面是应用上面代码的一个示例(非完整):

 1 None.gif private   void  Form7_Load( object  sender, System.EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3ExpandedSubBlockStart.gifContractedSubBlock.gif            string[] etc = new string[]dot.gif{"jpg""gif""bmp""tif""png"};
 4InBlock.gif            string path = @"C:\Documents and Settings\sh12\My Documents\My Pictures\NeweggLogo.";
 5InBlock.gif            try
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 7InBlock.gif                byte[] myImage = null;
 8InBlock.gif
 9InBlock.gif                Dataset2 ds = new Dataset2();
10InBlock.gif                DataRow drNew = null;
11InBlock.gif                for(int i=0; i<etc.Length; i++)
12ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
13InBlock.gif                    myImage = Newegg.Common.ImageUtil.ReadImage(path + etc);
14InBlock.gif
15InBlock.gif                    drNew = ds.MyTable.NewRow();
16InBlock.gif                    drNew["ImageType"= etc;
17InBlock.gif                    drNew["ImageData"= myImage;
18InBlock.gif                    ds.MyTable.Rows.Add(drNew);
19ExpandedSubBlockEnd.gif                }

20InBlock.gif
21InBlock.gif                myImage = Newegg.Common.ImageUtil.ReadImage(@"C:\Documents and Settings\sh12\My Documents\My Pictures\untitled.JPG");
22InBlock.gif                drNew = ds.MyTable.NewRow();
23InBlock.gif                drNew["ImageType"= "untitled.JPG";
24InBlock.gif                drNew["ImageData"= myImage;
25InBlock.gif                ds.MyTable.Rows.Add(drNew);
26InBlock.gif
27InBlock.gif                ds.AcceptChanges();
28InBlock.gif
29InBlock.gif                CrystalReport1 rpt = new CrystalReport1();
30InBlock.gif                rpt.SetDataSource(ds);
31InBlock.gif                this.crystalReportViewer1.ReportSource = rpt;
32ExpandedSubBlockEnd.gif            }

33InBlock.gif            catch(Exception ex)
34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
35InBlock.gif                MessageBox.Show(ex.Message);
36ExpandedSubBlockEnd.gif            }

37ExpandedBlockEnd.gif        }

其实这个实现非常简单,思路也容量想到。只是,还是希望能够给大家提供一些帮助或启迪了。

谢谢大家的阅读!

请指正! 

转载于:https://www.cnblogs.com/oosnoopy/archive/2005/12/17/299022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值