C# 加载具有内存效率的大图像

C# 加载具有内存效率的大图像,c#,.net,image,memory,C#,.net,Image,Memory,我正在使用.NET4.5、Windows窗体和C# 我正在使用以下命令将图像加载到按钮上: theButton.BackgroundImage = Image.FromFile("file.png"); 问题是我的按钮是128x128,图像是4000x8000。由于file.png太大,上面这行占用了大量内存 有人知道我可以使用什么技术来减少内存占用吗?我在考虑这样的函数: Image.FromFile(file,width,height); 有什么建议吗?谢谢。我认为最好的方法是将图像大小

我正在使用.NET4.5、Windows窗体和C#

我正在使用以下命令将图像加载到按钮上:

theButton.BackgroundImage = Image.FromFile("file.png");
问题是我的按钮是128x128,图像是4000x8000。由于file.png太大,上面这行占用了大量内存

有人知道我可以使用什么技术来减少内存占用吗?我在考虑这样的函数:

Image.FromFile(file,width,height);

有什么建议吗?谢谢。

我认为最好的方法是将图像大小调整为128x128。 那么大的图像总是会占用大量内存,不管你如何处理它


这也将允许您使图像在该尺寸下看起来很好。

这是一个相当普遍的问题,因为您几乎没有任何可能性

  • 在上传之前压缩图像,在现实世界中这是行不通的
  • 检查一下图片的大小和尺寸,在现实世界中它是有效的,即使是linkedin、facebook,他们也不允许我们上传超过指定尺寸的图片
  • 使用缓冲,这是在.net中最干净的方法
  • 使用一些第三方插件或开发环境,我已经在Silverlight中完成了

  • 是的,它有效。调整图像大小,然后在按钮上显示它,这非常简单

    但是,我不认为上面的代码保持了图像的纵横比

    使用纵横比调整图像大小非常简单;然后在按钮上显示它。 下面是通过保持纵横比帮助您调整图像大小的示例代码。 可以在现有类中定义新类或实现“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;
        }
    }
    

    我可能会加载图像,调整其大小,处理原始图像并使用调整大小的图像。如果你仍然起诉同一张图片,试着把它缓存在某个地方;这听起来是一个优雅的解决方案。你能告诉我如何处理原始图像吗?这在.NET上可能吗?MSRS比我快:)看他的回答:谢谢尼普,你有关于第3点的更多信息吗?其他解决方案目前对我来说都不可行。试试这个:谢谢你的回答-问题是源文件是由用户选择的,因此我无法事先调整大小。我已经将您的解决方案用于图像由我控制的其他场景,这非常有效,但不适用于我目前的情况。。
    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();
    }