0

.NET4.5、Windows フォーム、および C# を使用しています。

次を使用してボタンに画像をロードしています。

theButton.BackgroundImage = Image.FromFile("file.png");

問題は、ボタンが 128x128 で、画像が 4000x8000 であることです。file.png が非常に大きいため、上記の行は大量のメモリを消費します。

このメモリフットプリントを削減するために使用できる手法を知っている人はいますか? 私はこのようないくつかの機能を考えています:

Image.FromFile(file,width,height);

ポインタはありますか?ありがとう。

4

3 に答える 3

2

はい、動作します。画像のサイズを変更してボタンに表示するのは非常に簡単です。

しかし、上記のコードは画像の縦横比を維持しているとは思いません。

アスペクト比を使用して画像のサイズを変更するのは非常に簡単です。ボタンに表示します。以下のサンプル コードは、縦横比を維持して画像のサイズを変更するのに役立ちます。新しいクラスを定義するか、既存のクラスに「ResizeImage」メソッドを実装できます。どちらでも快適です。

public class ImageManipulation
{
    public static Bitmap ResizeImage(Bitmap originalBitmap, int newWidth, int maxHeight, bool onlyResizeIfWider)
    {
        if (onlyResizeIfWider)
        {
            if (originalBitmap.Width <= newWidth)
            {
                newWidth = originalBitmap.Width;
            }
        }

        int newHeight = originalBitmap.Height * newWidth / originalBitmap.Width;
        if (newHeight > maxHeight)
        {
            // Resize with height instead
            newWidth = originalBitmap.Width * maxHeight / originalBitmap.Height;
            newHeight = maxHeight;
        }

        var alteredImage = new Bitmap(originalBitmap, new Size(newWidth, newHeight));
        alteredImage.SetResolution(72, 72);
        return alteredImage;
    }
}

利用方法:

private void DisplayPhoto()
{
    // make sure the file is JPEG or GIF
                System.IO.FileInfo testFile = new System.IO.FileInfo(myFile);

    // Create a new stream to load this photo into
                FileStream myFileStream = new FileStream(myFile, FileMode.Open, FileAccess.Read);

    // Create a buffer to hold the stream of bytes
                photo = new byte[myFileStream.Length];
                // Read the bytes from this stream and put it into the image buffer
                myStream.Read(photo, 0, (int)myFileStream.Length);
                // Close the stream
                myFileStream.Close();

    // Create a new MemoryStream and write all the information from
            // the byte array into the stream
            MemoryStream myStream = new MemoryStream(photo, true);
            myStream.Write(photo, 0, photo.Length);

            // Use the MemoryStream to create the new BitMap object
            Bitmap FinalImage = new Bitmap(myStream);
            upicPhoto.Image = ImageManipulation.ResizeImage(
                                                FinalImage,
                                                upicPhoto.Width,
                                                upicPhoto.Height,
                                                true);


            // Close the stream
            myStream.Close();
}
于 2012-10-25T14:04:53.580 に答える
0

ここでの最善の方法は、画像のサイズを 128x128 に変更することだと思います。大きな画像は、何をするにしても、常に多くのメモリを占有します。

これにより、そのサイズで見栄えのする画像を作成することもできます。

于 2012-10-25T13:35:53.773 に答える
0

これは非常に一般的な問題です。私の知る限り、可能性はほとんどありません

  1. アップロードする前に画像を圧縮します。実際には、これは機能しません。
  2. 画像のサイズと寸法にチェックを入れてください。実際には機能します。リンクイン、Facebook でさえ、指定された寸法を超える画像をアップロードすることはできません。
  3. バッファリングを使用します。これは、.net で実行できる最もクリーンな方法です。
  4. いくつかのサードパーティのプラグインまたは開発環境を使用します。私は Silverlight でそれを行いました
于 2012-10-25T13:37:03.287 に答える