这篇文章主要围绕是否有原因Image.FromFile抛出无效图像格式的OutOfMemoryException?展开,旨在为您提供一份详细的参考资料。我们将全面介绍是否有原因Image.FromFi
这篇文章主要围绕是否有原因Image.FromFile抛出无效图像格式的OutOfMemoryException?展开,旨在为您提供一份详细的参考资料。我们将全面介绍是否有原因Image.FromFile抛出无效图像格式的OutOfMemoryException?,同时也会为您带来"System.OutOfMemoryException" exception when you execute a query in SQL Server Manageme...、.net – 创建数组时的SystemOutOfMemoryException、.net – 移动设备上的OutOfMemoryException、android – ImageView OutofMemoryException的实用方法。
本文目录一览:- 是否有原因Image.FromFile抛出无效图像格式的OutOfMemoryException?
- "System.OutOfMemoryException" exception when you execute a query in SQL Server Manageme...
- .net – 创建数组时的SystemOutOfMemoryException
- .net – 移动设备上的OutOfMemoryException
- android – ImageView OutofMemoryException
是否有原因Image.FromFile抛出无效图像格式的OutOfMemoryException?
我正在编写捕获此问题OutOfMemoryException
并引发新的,更直观的异常的代码:
/// .../// <exception cref="FormatException">The file does not have a valid image format.</exception>public static Image OpenImage( string filename ){ try { return Image.FromFile( filename ); } catch( OutOfMemoryException ex ) { throw new FormatException( "The file does not have a valid image format.", ex ); }}
此代码是否为用户所接受,还是OutOfMemoryException
由于特定原因而有意抛出?
答案1
小编典典不,这是历史。在.NET出现之前,就已经编写了GDI +一段时间。它的SDK包装器是用C 编写的。在C
中,异常是很容易的,并不是每个人都认可它们。谷歌没有。因此,为了使其兼容,它会报告错误代码问题。这永远无法很好地扩展,库程序员将故意限制可能的错误代码的数量作为目标,这减轻了客户端程序员必须报告错误代码的负担。
GDI +冒犯了这个问题,它仅定义20个错误代码。对于具有如此多的外部依赖项的大量代码而言,这 并不
多。这本身就是一个问题,有成千上万种方法来弄乱图像文件。库的错误报告无法细粒度地覆盖所有错误。在.NET定义标准Exception派生类型之前很久就已经选择了这些错误代码,这一事实当然无济于事。
Status ::
OutOfMemory错误代码被重载以表示不同的意思。有时确实确实意味着内存不足,它无法分配足够的空间来存储位图位。可悲的是,相同的错误代码报告了图像文件格式问题。这里的摩擦是它无法确定从图像文件读取的宽度高度像素是否是一个问题,因为没有足够的存储空间用于位图。或者,如果图像文件中的数据为垃圾数据。它假定图像文件不是垃圾文件,公平的调用,这是另一个程序的问题。所以OOM是它报告的内容。
为了完整起见,这些是错误代码:
enum Status{ Ok = 0, GenericError = 1, InvalidParameter = 2, OutOfMemory = 3, ObjectBusy = 4, InsufficientBuffer = 5, NotImplemented = 6, Win32Error = 7, WrongState = 8, Aborted = 9, FileNotFound = 10, ValueOverflow = 11, AccessDenied = 12, UnknownImageFormat = 13, FontFamilyNotFound = 14, FontStyleNotFound = 15, NotTrueTypeFont = 16, UnsupportedGdiplusVersion = 17, GdiplusNotInitialized = 18, PropertyNotFound = 19, PropertyNotSupported = 20,#if (GDIPVER >= 0x0110) ProfileNotFound = 21,#endif //(GDIPVER >= 0x0110)};
"System.OutOfMemoryException" exception when you execute a query in SQL Server Manageme...
Symptoms
When you use Microsoft SQL Server Management Studio (SSMS) to run an SQL query that returns a large amount of data, you receive an error message that resembles the following:
An error occurred while executing batch. Error message is: Exception of type ''System.OutOfMemoryException'' was thrown
Cause
This issue occurs because SSMS has insufficient memory to allocate for large results.
Note SSMS is a 32-bit process. Therefore, it is limited to 2 GB of memory. SSMS imposes an artificial limit on how much text that can be displayed per database field in the results window. This limit is 64 KB in "Grid" mode and 8 KB in "Text" mode. If the result set is too large, the memory that is required to display the query results may surpass the 2 GB limit of the SSMS process. Therefore, a large result set can cause the error that is mentioned in the "Symptoms" section.
Workaround
To work around this issue, try one of the following methods.
Method 1: Output the results as text
Configure the query window to output the query results as text. A text output uses less memory than the grid, and it may be sufficient to display the query results. To make this change, follow these steps:
- Right-click the query window.
- Click Results to.
- Click Results to Text.
Method 2: Output the results to a file
Configure the query window to output the query results to a file. A file output uses a minimal amount of memory. This reserves more memory for storing the results set. To make this change, follow these steps:
- Right-click the query window.
- Click Results to.
- Click Results To File.
- Run the query, and then select the location in which to save the results file.
Method 3: Use sqlcmd
Use the sqlcmd tool instead of SSMS to run the SQL queries. This method enables queries to be run without the resources that are required by the SSMS UI. Additionally, you can use the 64-bit version of Sqlcmd.exe to avoid the memory restriction that affects the 32-bit SSMS process.
原文链接
.net – 创建数组时的SystemOutOfMemoryException
这是代码(请不要判断代码,不是我的代码至少7年)
Dim myFiletoUpload As New IO.FileInfo(IO.Path.Combine(m_Path,filename)) Dim myFileStream As IO.FileStream Try myFileStream = myFiletoUpload.OpenRead Dim bytes As Long = myFileStream.Length //(Length is roughly 308 million) If bytes > 0 Then Dim data(bytes - 1) As Byte // OutOfMemoryException is caught here myFileStream.Read(data,bytes) objInfo.content = data End If Catch ex As Exception Throw ex Finally myFileStream.Close() End Try
根据这个问题“SO Max Size of .Net Arrays”和这个问题“Maximum lenght of an array”,最大长度为2,147,483,647个元素或Int32.MaxValue,最大大小为2 GB
所以我的阵列的总长度完全在限制范围内(3.08亿< 20亿),而且我的大小也小于2 GB(文件大小为298 mb). 题:
所以我的问题,关于数组还有什么可能导致MemoryOutOfMemoryException?
注意:对于那些想知道服务器仍然有一些10GB的免费ram空间
注2:在dude’s advice之后,我在几次运行中监控了GDI对象的数量.进程本身永远不会超过计数1500个对象.
例如,在我的机器上,我不能在一个阵列中分配超过908 000 000字节,但如果存储在更多阵列中,我可以毫无问题地分配100 * 90 800 000:
// alocation in one array byte[] toBigArray = new byte[908000000]; //doesn't work (6 zeroes after 908) // allocation in more arrays byte[][] a=new byte[100][]; for (int i = 0 ; i<a.Length;i++) // it works even there is 10x more memory needed than before { a[0] = new byte[90800000]; // (5 zeroes after 908) }
.net – 移动设备上的OutOfMemoryException
public static Bitmap TakePicture() { var dialog = new CameraCaptureDialog { Resolution = new Size(1600,1200),StillQuality = CameraCaptureStillQuality.Default }; dialog.ShowDialog(); // If the filename is empty the user took no picture if (string.IsNullOrEmpty(dialog.FileName)) return null; // (!) The OutOfMemoryException is thrown here (!) var bitmap = new Bitmap(dialog.FileName); File.Delete(dialog.FileName); return bitmap; }
该函数由事件处理程序调用:
private void _pictureBox_Click(object sender,EventArgs e) { _takePictureLinkLabel.Visible = false; var image = Camera.TakePicture(); if (image == null) return; image = Camera.CutBitmap(image,2.5); _pictureBox.Image = image; _image = Camera.ImagetoByteArray(image); }
解决方法
同样地,你可能应该使用图像,但是再次:不确定我是否期望这是成败;值得一试,但……
public static Bitmap TakePicture() { string filename; using(var dialog = new CameraCaptureDialog { Resolution = new Size(1600,StillQuality = CameraCaptureStillQuality.Default }) { dialog.ShowDialog(); filename = dialog.FileName; } // If the filename is empty the user took no picture if (string.IsNullOrEmpty(filename)) return null; // (!) The OutOfMemoryException is thrown here (!) var bitmap = new Bitmap(filename); File.Delete(filename); return bitmap; } private void _pictureBox_Click(object sender,EventArgs e) { _takePictureLinkLabel.Visible = false; using(var image = Camera.TakePicture()) { if (image == null) return; image = Camera.CutBitmap(image,2.5); _pictureBox.Image = image; _image = Camera.ImagetoByteArray(image); } }
我也会对CutBitmap等有点谨慎,以确保尽快发布.
android – ImageView OutofMemoryException
在ImageView中插入图像时遇到问题.我已经尝试了几种方法来解决它,但它仍然失败,最新版本如下.图像大小为3000×3000.
码:
ImageView iv = (ImageView) findViewById(R.id.imageView);
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.prueba_plano_2).copy(Bitmap.Config.ARGB_8888, true);
iv.setimageBitmap(originalBitmap);
XML:
<FrameLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/buttonlayer" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/desc"
android:scaleType="matrix" />
</FrameLayout>
logcat的:
06-21 09:32:06.721: E/AndroidRuntime(25709): FATAL EXCEPTION: main
06-21 09:32:06.721: E/AndroidRuntime(25709): java.lang.OutOfMemoryError
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.Bitmap.nativeCreate(Native Method)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
06-21 09:32:06.721: E/AndroidRuntime(25709): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:404)
06-21 09:32:06.721: E/AndroidRuntime(25709): at
.............
解决方法:
位图的最大大小为2048×2048,如果您的位图大于此值,则应缩放它.
我正在使用此方法来缩放位图:
public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {
if(bitmapToScale == null)
return null;
//get the original width and height
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(newWidth / width, newHeight / height);
// recreate the new Bitmap and set it back
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
}
您还可以使用以下方法检查内存状态:
ActivityManager actMgr = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo minfo = new ActivityManager.MemoryInfo();
actMgr.getMemoryInfo(minfo);
if(minfo.lowMemory) { //do something}
今天关于是否有原因Image.FromFile抛出无效图像格式的OutOfMemoryException?的分享就到这里,希望大家有所收获,若想了解更多关于"System.OutOfMemoryException" exception when you execute a query in SQL Server Manageme...、.net – 创建数组时的SystemOutOfMemoryException、.net – 移动设备上的OutOfMemoryException、android – ImageView OutofMemoryException等相关知识,可以在本站进行查询。
本文标签: