请帮助我更正代码 [英] Please help me to correct the code

查看:97
本文介绍了请帮助我更正代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
            {

                var fi = new FileInfo(fileName);
                var dimension = (int)Math.Sqrt((fi.Length + 1d) / 4);
                using (var bitmap = new Bitmap(dimension, dimension + 2))
                {
                    
                    Array.Copy(File.ReadAllBytes(fileName), buffer, fi.Length);
                    int x = 1, y = 0;
                    for (var offset = 0; offset < buffer.Length; offset += 4)
                    {
                        var colorValue = BitConverter.ToInt32(buffer, offset);
                        bitmap.SetPixel(x, y, Color.FromArgb(colorValue));
                       
                    }
                    bitmap.Save(Path.ChangeExtension(fileName, ".gif"), ImageFormat.Gif);
                }

            }catch(OutOfMemoryException)
            {}

推荐答案

如果我是你,我会认真考虑重新设计它. .NET内置的单个对象大小限制为2GB.
请记住,您正在创建文件大小的三个对象(读取的字节+缓冲区+位图,可能很难找到足够大的连续块来容纳它们.并且其中不包括任何空间系统需要将其作为GIF文件...

您要做什么,实际上需要像这样处理-忽略了事实,无论如何都要在千兆位位图上与SetPixel玩一样慢?
If I were you, I would seriously think about re-designing this. .NET has a single-object size limit built in at 2GB.
Bear in mind that you are creating three objects the size of your file (the bytes read + buffer + the bitmap, and it probably gets kinda difficult to find a big enough contiguous block to hold them. And that doesn''t include whatever space the system needs to gave it as a GIF file...

What are you trying to do that you really need to handle like this - ignoring the fact that it will be as slow as heck playing with SetPixel on a gigabyte bitmap anyway?


这篇关于请帮助我更正代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆