2013/08/12
出问题的代码:
private bool DevideImageIngnoreHalfWord(List<Image> bmpList, Size wndSize, out List<Image> newBmpList)
{
newBmpList = new List<Image>();
for (int i = 0; i < bmpList.Count; i++)
{
Bitmap bmpToWnd = new Bitmap(bmpList[i], new Size(wndSize.Width, bmpList[i].Height));
int bmpCount = 0;
int lastBmpHeight = 0;
int curBmpPosY = 0;
Size safeSize = new Size(0, 0);
Bitmap newBitmap;
bmpCount = bmpToWnd.Height / wndSize.Height;
lastBmpHeight = bmpToWnd.Height % wndSize.Height;
if (bmpCount == 0)
{
newBitmap = new Bitmap(bmpToWnd);
if (!IsBmpHaveNoContent(newBitmap))
{
newBmpList.Add(newBitmap);
}
else
{
if (newBitmap != null)
{
newBitmap.Dispose();
newBitmap = null;
}
}
if (bmpToWnd != null)
{
bmpToWnd.Dispose();
bmpToWnd = null;
}
continue;
}
for (int j = 0; j < bmpCount; j++)
{
newBitmap = new Bitmap(wndSize.Width, wndSize.Height);
Graphics gNewBmp = Graphics.FromImage(newBitmap);
Point point = new Point(0, curBmpPosY);
gNewBmp.DrawImage(bmpToWnd, 0, 0, new Rectangle(point, wndSize), GraphicsUnit.Pixel);
if (!IsBmpHaveNoContent(newBitmap))
{
newBmpList.Add(newBitmap);
}
else
{
if (newBitmap != null)
{
newBitmap.Dispose();
newBitmap = null;
}
}
curBmpPosY += wndSize.Height;
if (gNewBmp != null)
{
gNewBmp.Dispose();
gNewBmp = null;
}
}
if (lastBmpHeight > 0)
{
newBitmap = new Bitmap(wndSize.Width, lastBmpHeight);
Graphics gNewBmp = Graphics.FromImage(newBitmap);
Point point = new Point(0, curBmpPosY);
Size size = new Size(wndSize.Width, lastBmpHeight);
gNewBmp.DrawImage(bmpToWnd, 0, 0, new Rectangle(point, size), GraphicsUnit.Pixel);
if (!IsBmpHaveNoContent(newBitmap))
{
newBmpList.Add(newBitmap);
}
else
{
if (newBitmap != null)
{
newBitmap.Dispose();
newBitmap = null;
}
}
if (gNewBmp != null)
{
gNewBmp.Dispose();
gNewBmp = null;
}
}
if (bmpToWnd != null)
{
bmpToWnd.Dispose();
bmpToWnd = null;
}
}
return true;
}
异常信息:
Exception:A generic error occurred in GDI+.
System.Runtime.InteropServices.ExternalException
Stack Trace:
at System.Drawing.Graphics.Clear(Color color)
at System.Drawing.Bitmap..ctor(Image original, Int32 width, Int32 height)
at System.Drawing.Bitmap..ctor(Image original)
at Nova.Rss.RssToImage.DevideImageIngnoreHalfWord(List`1 bmpList, Size wndSize, List`1& newBmpList)
重要参考资料:
http://support.microsoft.com/kb/814675/zh-cn