调整图像大小并放置在CrystalReports BlobField的字节数组中

时间:2013-02-23 03:23:27

标签: arrays vb.net image crystal-reports blob

我试图搜索这个并且代码应该尽我所能,但我的Crystal Report中的结果图像是5页而不是1由于某种原因!

基本上,我有一个Crystal报告,其中从BlobField获取的整页图像在源图像宽2409像素,高3436像素@ 300 dpi时效果很好。

当我尝试使用1700宽,2436高@ 200 dpi的源图像时,图像高度太大,并将报告挂在下一页上

我认为“没问题,我只会调整图像大小并且报告会正确显示”但是我这样做有很大困难。这是我正在使用的代码 - 当使用“正常”时“图像大小和这段代码,一切都在报告中显示得很好但是如果我需要调整大小,它会在很大的范围内延伸到五页以上,这比单独留下更糟糕! :(

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

也许我正在解决这个问题,但基本上我正试图找到一种方法,允许将任何大小的图像放入Crystal Reports BlobField并让它们占用一整页A4。

1 个答案:

答案 0 :(得分:0)

您应该已将图像(如byte [])存储在某处,将其传递给此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;
}

FixedSize函数:

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