我是C#的新手。 因此,我不太确定我的程序有什么问题。 该程序适用于小图像,但在与大小约为A4尺寸的大图像一起使用时,会显示“Out of memory exeception”。 但是,如果程序无法处理大图像,该程序将毫无用处。 我该如何解决这个问题? 谢谢。C中的内存不足异常#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
//Bitmap objects
//input image
Bitmap bmOrg = (Bitmap)Bitmap.FromFile(@"C:\B.png");
Bitmap bmTransparentLayover = new Bitmap(bmOrg.Width, bmOrg.Height);
//Create Graphic Objects.
Graphics gOriginal = Graphics.FromImage(bmOrg);
Graphics gTransparentLayover = Graphics.FromImage(bmTransparentLayover);
//Set Transparent Graphics back ground to an "odd" color
// that hopefully won't be used to
//Be changed to transparent later.
gTransparentLayover.FillRectangle
(Brushes.Pink,
new Rectangle
(0,
0,
bmTransparentLayover.Width,
bmTransparentLayover.Height
)
);
//Draw "transparent" graphics that will look through
// the overlay onto the original.
//Using LimeGreen in hopes that it's not used.
Point[] points = new Point[5];
points[0] = new Point(130, 140);
points[1] = new Point(130, 370);
points[2] = new Point(420, 370);
points[3] = new Point(420, 140);
points[4] = new Point(130, 140);
System.Drawing.Drawing2D.GraphicsPath gp = new
System.Drawing.Drawing2D.GraphicsPath();
gp.AddPolygon(points);
gTransparentLayover.FillPath(Brushes.LimeGreen, gp);
//Now make the LimeGreen Transparent to see through overlay.
bmTransparentLayover.MakeTransparent(Color.LimeGreen);
//draw the overlay on top of the original.
gOriginal.DrawImage(bmTransparentLayover,
new Rectangle(0, 0, bmTransparentLayover.Width, bmTransparentLayover.Height));
//Create new image to make the overlays background tranparent
Bitmap bm3 = new Bitmap(bmOrg);
bm3.MakeTransparent(Color.Pink);
//Save file.
//to save the output image
bm3.Save(@"save.png",System.Drawing.Imaging.ImageFormat.Png);
Image img = new Bitmap(480, 480);
//the background image
img = Image.FromFile(@"a.png");
Graphics g = Graphics.FromImage(img);
//to save the combined image
g.DrawImage(Image.FromFile(@"save.png"), new Point(-50, -70));
img.Save(@"final.png", ImageFormat.Png);
}
}
}
无关回答你的问题,你必须知道你必须关闭和处理你的图形对象。使用'using'语句填充代码段。那会做这项工作。 – 2012-07-19 08:21:34
A4大小不会说什么。我可以创建一个只有4个像素的A4尺寸图像:) – 2012-07-19 08:21:38
不确定问题所在,但是尝试在实现IDisposable(位图,图形)的每个对象周围添加使用语句。 当您查看任务管理器时,进程使用多少内存? – Maarten 2012-07-19 08:22:32