0

I am using .NET4.5, Windows Forms and C#.

I am loading an image onto a button using:

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

The issue is that my button is 128x128 and the image is 4000x8000. The line above consumes very large amounts of memory because file.png is so large.

Does anyone know of a technique I can use to reduce this memory footprint? I am thinking of some function like this:

Image.FromFile(file,width,height);

Any pointers? Thanks.

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

这是一个相当普遍的问题,AFAIK你几乎没有可能性

  1. 在上传之前压缩图像,在现实世界中这是行不通的。
  2. 检查图像的大小和尺寸,在现实世界中它可以工作,即使是linkedin、facebook,它们也不允许我们上传超过指定尺寸的图像。
  3. 使用缓冲,这是您可以在 .net 中执行的最干净的方法
  4. 使用一些第三方插件或开发环境,我在 Silverlight 中做过
于 2012-10-25T13:37:03.287 回答