views:

409

answers:

1

I'm working with a Crystal Reports object in Visual Studio 2008 (C#). The report is building fine and the data is binding correctly. However, when I try to resize an IBlobFieldObject from within the source, the scale is getting skewed.

Two notes about this scenario. Source image is 1024x768, my max width and height are 720x576. My math should be correct that my new image size will be 720x540 (to fit within the max width and height guidelines). The ratio is wrong when I do this though:

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;
}

I've stepped through the code to make sure that the values are correct from the math, and I've even saved the image file out to make sure that the aspect ratio is correct (it was). No matter what I try though, the image is squashed--almost as if the Scale values are off in the Crystal Reports designer (they're not). Thanks in advance for any help!

A: 

There are several problems with the way Crystal Reports handles IBlobFieldObjects. The first problem that I had was that the inline documentation was incorrect for a Crystal Reports' ReportObjects' Height and Width properties. It says that the values are stated in twips, which they AREN'T. For example:

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

The second problem had to do with the imageToByteArray conversion I was doing. Here is the method I was using:

    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();
    }

In summation, it appears that Crystal Reports prefers ImageFormat.Bmp for filling IBlobFieldObjects. Now if somebody could tell me how to fix the awful bitdepth of using ImageFormat.Bmp (it's most likely the way that the Crystal Reports Report object handles the image data, and may not be fixable), I'd be all set.

C. Griffin