2010-05-07 99 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對象處理圖像數據的方式,並且可能無法解決),我會全部設置好。