2013-02-23 33 views
5

我試圖搜索此代碼,並應該儘可能地工作,但我的Crystal Report中生成的圖像是5頁而不是1一些原因!調整圖像大小並將其放置在CrystalReports的字節數組中BlobField

基本上,我有一個Crystal報告,其中包含從BlobField中獲取的完整頁面圖像,當源圖像寬度爲2409像素且高度爲3436像素@ 300 dpi時,該報告完美地起作用。

當我嘗試用2436高@ 200個dpi的使用源圖像是1700寬,圖像高度是太大,掛落報告到下一頁有點

我想:「沒問題,我只是調整圖像大小,並且報告將正確顯示「但我在這方面遇到了很大的困難..下面是我正在使用的代碼 - 使用」正常「圖像大小和此代碼時,所有內容都顯示在報告中沒有問題,但如果我需要調整大小,它會大幅擴展並超過五頁,這比放棄它更糟! :(

Dim fs As System.IO.FileStream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read) 
Dim Image() As Byte = New Byte(fs.Length - 1) {} 
fs.Read(Image, 0, CType(fs.Length, Integer)) 
fs.Close() 

'Byte[] to image 
Dim imgMemoryStream = New IO.MemoryStream(Image) 
Dim myImage = Drawing.Image.FromStream(imgMemoryStream) 

' Check if image is 2409 wide, if it's not then resize to 2409 while preserving aspect ratio. WIN. 
If myImage.Width <> 2409 Then 
    MsgBox("myimage before: " & myImage.Width & " by " & myImage.Height) 
    myImage = ImageResize(myImage, 3436, 2409) 
    MsgBox("myimage after: " & myImage.Width & " by " & myImage.Height) 
Else 
    MsgBox("myimage (already correct for printing): " & myImage.Width & " by " & myImage.Height) 
End If 

Dim imgMemoryStream2 As IO.MemoryStream = New IO.MemoryStream() 
myImage.Save(imgMemoryStream2, System.Drawing.Imaging.ImageFormat.Jpeg) 
Image = imgMemoryStream2.ToArray 

objDataRow(strImageField) = Image 

所以我抓住了原始圖像成字節數組(如我假設圖像尺寸爲「正常」在默認情況下,將剛插入直入BlobField),然後將其放回一個圖像來檢查圖像大小如果大小不是「正常」,那麼我將調整圖像大小,然後將其轉換回字節數組以供給報告中的BlobField

這是圖像大小調整代碼:

Public Shared Function ImageResize(ByVal image As System.Drawing.Image, _ 
ByVal height As Int32, ByVal width As Int32) As System.Drawing.Image 
Dim bitmap As System.Drawing.Bitmap = New System.Drawing.Bitmap(width, height, image.PixelFormat) 
If bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format1bppIndexed Or _ 
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format4bppIndexed Or _ 
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format8bppIndexed Or _ 
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Undefined Or _ 
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.DontCare Or _ 
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppArgb1555 Or _ 
bitmap.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppGrayScale Then 
Throw New NotSupportedException("Pixel format of the image is not supported.") 
End If 
Dim graphicsImage As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap) 
graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality 
graphicsImage.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic 
graphicsImage.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height) 
graphicsImage.Dispose() 
Return bitmap 
End Function 

也許我正在解決的問題不正確,但基本盟友我試圖找到一種方法,允許任何大小的圖像放入水晶報表BlobField,讓他們佔用一個完整的A4頁面。

回答

0

您應該將圖像(作爲字節[])存儲在某個位置,將其傳遞給此ResizeBytes函數,以及您希望返回圖像的新尺寸。

private byte[] ResizeBytes(byte[] byteImageIn, int NewWidth, int NewHeight) 
{ 
    //Convert Bytes to Image 
    MemoryStream ms1 = new MemoryStream(byteImageIn); 
    Image img = Image.FromStream(ms1); 

    //Convert Image in to new image with new dimensions, padding with a white background 
    img = FixedSize(img, NewWidth, NewHeight); 

    //Convert image back to a byte array 
    MemoryStream ms2 = new MemoryStream(); 
    img.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg); 
    byte[] imageBytes = ms2.ToArray(); 
    return imageBytes; 
} 

的固定大小功能:

private Image FixedSize(Image imgPhoto, int Width, int Height) 
{ 
    int sourceWidth = imgPhoto.Width; 
    int sourceHeight = imgPhoto.Height; 
    int sourceX = 0; 
    int sourceY = 0; 
    int destX = 0; 
    int destY = 0; 

    float nPercent = 0; 
    float nPercentW = 0; 
    float nPercentH = 0; 

    nPercentW = ((float)Width/(float)sourceWidth); 
    nPercentH = ((float)Height/(float)sourceHeight); 
    if (nPercentH < nPercentW) 
    { 
     nPercent = nPercentH; 
     destX = System.Convert.ToInt16((Width - 
         (sourceWidth * nPercent))/2); 
    } 
    else 
    { 
     nPercent = nPercentW; 
     destY = System.Convert.ToInt16((Height - 
         (sourceHeight * nPercent))/2); 
    } 

    int destWidth = (int)(sourceWidth * nPercent); 
    int destHeight = (int)(sourceHeight * nPercent); 

    Bitmap bmPhoto = new Bitmap(Width, Height, 
         PixelFormat.Format48bppRgb); //Format24bppRgb 
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution, 
        imgPhoto.VerticalResolution); 

    Graphics grPhoto = Graphics.FromImage(bmPhoto); 
    grPhoto.Clear(Color.White); 
    grPhoto.InterpolationMode = 
      InterpolationMode.HighQualityBicubic; 

    grPhoto.DrawImage(imgPhoto, 
     new Rectangle(destX, destY, destWidth, destHeight), 
     new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
     GraphicsUnit.Pixel); 

    grPhoto.Dispose(); 
    return bmPhoto; 
} 
+0

非常感謝您的回覆戴夫! 我實際上最終修改了顯示圖像的報告對象,但我仍然非常感謝您提供這個問題的直接答案:) 我會回頭看看代碼,看看你是怎麼做的。 你能告訴我我嘗試使用的代碼有什麼問題嗎?它有msgbox確認我調整的分辨率是我想要的,但是當我將這個調整大小的blob放入報告中時,它非常龐大! – user2101511 2013-04-15 09:26:01

+0

我尋找了很長時間來解決這個問題,甚至把任務委派給我的報告人員,他無法弄清楚只使用水晶報告的解決方案。所以我們必須拿出上述來解決這個問題,它對我們來說很好。但是,我們使用的是小公司徽標等。調整大小後質量略有下降,並且由於我們的使用情況,這不是什麼大問題。你的milage可能會有所不同。 – 2013-04-15 23:43:41

相關問題