Crystal Reports程序化图像缩放...比例?

3
我正在使用Visual Studio 2008(C#)中的Crystal Reports对象。报告构建良好,数据绑定正确。但是,当我尝试从源代码调整IBlobFieldObject大小时,比例会失真。
关于此情况有两点需要注意。源图像为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;
}

我已经逐步检查代码,确保从数学上计算出的值是正确的,并且甚至保存了图像文件以确保其宽高比正确(确实如此)。无论我尝试什么,图像都被压缩了——几乎像是Crystal Reports设计者中的比例值错误一样(但它们并不是)。非常感谢您提前的帮助!

1个回答

3

水晶报表处理IBlobFieldObjects的方式存在几个问题。我遇到的第一个问题是,Crystal Reports的ReportObjects的Height和Width属性的内联文档不正确。它说值以twips为单位,但实际上不是。例如:

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 报表对象处理图像数据的方式),那就太好了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接