5

私はこれを検索しようとしましたが、私のCrystal Reportの結果の画像は何らかの理由で1ページではなく5ページです!

基本的に、ソース画像が幅2409ピクセル、高さ3436ピクセル@ 300 dpiの場合に完全に機能するBlobFieldから取得したフルページ画像を含むCrystal Reportがあります。

幅 1700、高さ 2436 @ 200 dpi のソース画像を使用しようとすると、画像の高さが大きすぎて、レポートが次のページに少しはみ出してしまいます

「問題ありません。画像のサイズを変更するだけで、レポートが正しく表示されます」と思いましたが、そうするのが非常に困難です。現在使用しているコードは次のとおりです-「通常の」画像サイズを使用する場合このコードでは、レポートではすべてが正常に表示されますが、サイズを変更する必要がある場合は、5 ページを超えて非常に広くなり、そのままにしておくよりもさらに悪いことになります。:(

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 ページ全体を占めるようにする方法を見つけようとしています。

4

1 に答える 1

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;
}
于 2013-04-15T05:17:14.217 に答える