3

グラフィカル アプリケーションを開発しており、各ページのサムネイルを保持する必要があります。課題は、パフォーマンスを落とさずにサムネイル ファイルを生成する方法です??

現在、これを行うための私のコードは次のとおりです。

VisualBrush VisualBrush = new VisualBrush(pageToGenerateThumbnailFor);
UIVisual.Background = VisualBrush;

RenderTargetBitmap = new RenderTargetBitmap((int)UIVisual.ActualWidth, (int)UIVisual.ActualHeight, 96, 96, PixelFormats.Pbgra32);

rtb.Render(UIVisual);

using (FileStream outStream = new FileStream(ThumbFileFullPath, FileMode.OpenOrCreate, 
System.IO.FileAccess.ReadWrite))
{
   PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
   pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
   pngEncoder.Save(outStream);
}

では、特定の Visual のサムネイルを生成するより高速な方法はありますか?

ありがとう

4

2 に答える 2

3

最近、e コマース サイトのイメージ サムネイルをオンザフライで生成するための調査を少し行いました。上記の回答と同様に、ビットマップを生成してからサイズ変更などを自分で行うことから始めました。ディスク上の画像サイズと品質に問題があった後、http://imageresizing.net/を調べましたが、それ以来、振り返っていません。次の 1 行のコードで、byte()、ストリーム、および物理ファイルからイメージをすべて非常に迅速に生成できます。

ImageBuilder.Current.Build(New MemoryStream(bImage), sImageLocation + sFullFileName, New      ResizeSettings("maxwidth=214&maxheight=238"))

車輪の再発明を試みるよりも、このコンポーネントをお勧めします...

于 2012-11-22T15:28:52.963 に答える
1

私が作成したユーティリティ ライブラリの次のクラスは、パフォーマンスが高く、鮮明な品質のサムネイルを生成します...

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

namespace Simple {
    public static class ThumbnailCreator {
        private static readonly object _lock = new object();

        public static Bitmap createThumbnail(Stream source, Int32 width, Int32 height) {
            Monitor.Enter(_lock);
            Bitmap output = null;
            try {
                using (Bitmap workingBitmap = new Bitmap(source)) {
                    // Determine scale based on requested height/width (this preserves aspect ratio)
                    Decimal scale;
                    if (((Decimal)workingBitmap.Width / (Decimal)width) > ((Decimal)workingBitmap.Height / (Decimal)height)) {
                        scale = (Decimal)workingBitmap.Width / (Decimal)width;
                    }
                    else {
                        scale = (Decimal)workingBitmap.Height / (Decimal)height;
                    }
                    // Calculate new height/width
                    Int32 newHeight = (Int32)((Decimal)workingBitmap.Height / scale);
                    Int32 newWidth = (Int32)((Decimal)workingBitmap.Width / scale);
                    // Create blank BitMap of appropriate size
                    output = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
                    // Create Graphics surface
                    using (Graphics g = Graphics.FromImage(output)) {
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        Rectangle destRectangle = new Rectangle(0, 0, newWidth, newHeight);
                        // Use Graphics surface to draw resized BitMap to blank BitMap
                        g.DrawImage(workingBitmap, destRectangle, 0, 0, workingBitmap.Width, workingBitmap.Height, GraphicsUnit.Pixel);
                    }
                }
            }
            catch {
                output = null;
            }
            finally {
                Monitor.Exit(_lock);
            }
            return output;
        }
    }
}

また、元の画像の縦横比も保持されます。

于 2012-11-22T14:41:30.773 に答える