我是C#的新手.因此,我不太确定我的程序存在什么问题.该程序适用于小图像,但当它处理大约A4大小的大图像时,它显示"内存不足".但是,如果程序无法处理大图像,那么该程序将毫无用处.我该如何解决这个问题?谢谢.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一张9992x8750的图片
是的,您处于32位进程的危险区域.该映像需要大量连续的地址空间,334 MB.在程序启动后立即加载图像时,您可以轻松获得该图像.你可以获得大约650兆字节,给予或接受.
但是当你的程序分配和释放内存并加载一些程序集时,那就会从那里下山.地址空间变得碎片化,分配之间的漏洞越来越小.只需加载具有笨拙基址的DLL,就可以突然将最大的漏洞削减两倍以上.问题不在于可用的虚拟内存总量,它可以是千兆字节或更多,它是最大可用块的大小.过了一会儿,90兆字节的分配失败并不完全是不寻常的.只要一个分配失败,你仍然可以分配一堆,比方说,50兆字节的块.
地址空间碎片无法解决问题,您无法修复Bitmap类存储像素数据的方式.但是,请指定64位版本的Windows作为程序的要求.它提供了大量的虚拟内存,碎片是一个非问题.如果您想追逐它,那么您可以使用SysInternals的VMMap实用程序深入了解VM布局.请注意您可能遇到的信息过载.
| 归档时间: |
|
| 查看次数: |
3395 次 |
| 最近记录: |