Crystal Reports Programmatic Image Resizing ... Scale?
I am working with Crystal Reports object in Visual Studio 2008 (C #). The report builds perfectly and the data is linked correctly. However, when I try to resize the IBlobFieldObject inside the source, the scale becomes distorted.
Two notes on this scenario. The original image is 1024x768, the maximum width and height are 720x576. My math needs to be correct so that my new image size is 720x540 (to meet the maximum width and height requirements). The ratio is wrong if I do this:
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 went through the code to make sure the values are correct from the math, and I even saved the image file to make sure the aspect ratio is correct (it was). Regardless of what I try, the image is compressed - almost as if the Scale values were turned off in the Crystal Reports designer (they are not). Thanks in advance for your help!
a source to share
There are several issues with how Crystal Reports handles IBlobFieldObjects. The first problem I ran into was that the inline documentation was wrong for the Height and Width properties of Crystal Reports ReportObjects. It says the values are in tweets they are NOT. For instance:
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 has to do with the imageToByteArray conversion I was doing. Here is the method I used:
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 summary, it looks like Crystal Reports prefers ImageFormat.Bmp for filling IBlobFieldObjects. Now if anyone can tell me how to fix the awful bit of code using ImageFormat.Bmp (most likely the way the Crystal Reports Report object handles image data and may not be fixable), I would install everything.
a source to share