2010-05-07 101 views
2

我正在使用Visual Studio 2008(C#)中的Crystal Reports对象。报告构建正常,数据绑定正确。但是,当我尝试从源代码中调整IBlobFieldObject的大小时,它会发生偏斜。Crystal Reports编程图像调整大小...比例?

有关此方案的两点注意事项。源图像是1024x768,我的最大宽度和高度是720x576。我的数学应该是正确的,我的新图像尺寸将为720x540(以适应最大宽度和高度指南)。该比率是错误的,当我这样做虽然:

img = Image.FromFile(path); 
newWidth = img.Size.Width; 
newHeight = img.Size.Height; 

if ((img.Size.Width > 720) || (img.Size.Height > 576)) 
{ 
    double ratio = Convert.ToDouble(img.Size.Width)/Convert.ToDouble(img.Size.Height); 
    if (ratio > 1.25) // Adjust width to 720, height will fall within range 
    { 
     newWidth = 720; 
     newHeight = Convert.ToInt32(Convert.ToDouble(img.Size.Height) * 720.0/Convert.ToDouble(img.Size.Width)); 
    } 
    else     // Adjust height to 576, width will fall within range 
    { 
     newHeight = 576; 
     newWidth = Convert.ToInt32(Convert.ToDouble(img.Size.Width) * 576.0/Convert.ToDouble(img.Size.Height)); 
    } 

    imgRpt.Section3.ReportObjects["image"].Height = newHeight; 
    imgRpt.Section3.ReportObjects["image"].Width = newWidth; 
} 

我已经通过代码加强,以确保该值是正确的从数学和我甚至保存图像文件出来,以确保纵横比是正确的(它是)。不管我尝试什么,图像都被压扁了 - 就好像水晶报表设计器中的“缩放”值已关闭(它们不是)。预先感谢任何帮助!

回答

2

Crystal Reports处理IBlobFieldObjects的方法有几个问题。我遇到的第一个问题是,对于Crystal Reports的ReportObjects的“高度和宽度”属性,内联文档不正确。它说价值以缇为单位,他们不是。例如:

ImageReport imgRpt = new ImageReport(); 
// The following value should be in PIXELS... NOT twips as the docs suggest! 
imgRpt.Section3.ReportObjects["image"].Height = 300; 

第二个问题曾与imageToByteArray转换我在做的事情。下面是我用的方法:

public byte[] imageToByteArray(System.Drawing.Image imageIn) 
    { 
     MemoryStream ms = new MemoryStream(); 
     // The following line was ImageFormat.Jpeg, but it caused sizing issues 
     // in Crystal Reports. Changing to ImageFormat.Bmp made the squashed 
     // problems go away. 
     imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 
     return ms.ToArray(); 
    } 

在求和,所以它看起来的Crystal Reports喜欢ImageFormat.Bmp填充IBlobFieldObjects。现在,如果有人能告诉我如何解决使用ImageFormat.Bmp的糟糕问题(这很可能是Crystal Reports Report对象处理图像数据的方式,并且可能无法解决),我会全部设置好。