C# 在整个应用程序中提供位图
我有一个应用程序,它在启动时读取GIF,从GIF中提取多个位图,然后在整个应用程序中提供这些位图,以便在其他类中绘制。例如:C# 在整个应用程序中提供位图,c#,image,bitmap,static,C#,Image,Bitmap,Static,我有一个应用程序,它在启动时读取GIF,从GIF中提取多个位图,然后在整个应用程序中提供这些位图,以便在其他类中绘制。例如: public class Images { public static Bitmap pic1, pic2, pic3; public static void MakeBitmaps(string pathToGif) { var bigPic = new Bitmap(pathToGif); pic1 = bigPic
public class Images
{
public static Bitmap pic1, pic2, pic3;
public static void MakeBitmaps(string pathToGif)
{
var bigPic = new Bitmap(pathToGif);
pic1 = bigPic.Clone(new Rectangle(1,1,100,100));
pic2 = bigPic.Clone(new Rectangle(101,1,100,100));
pic3 = bigPic.Clone(new Rectangle(201,1,100,100));
}
}
这样,我只需使用以下命令即可访问其他类中的所有三个位图:
g.DrawImage(Images.pic1, x, y)
g.DrawImage(Images.pic2, x, y)
等等
我以这种方式打开了几个GIF,并从中提取子图像以用于我的程序。最后有很多这样的位图,即使它们很小(小于100x100像素)
虽然这样做很好,但有没有更好的方法?我是否应该避免使用Bitmaps的静态修饰符,当有多个位图时,它会引起内存问题吗? 在需要全局性的情况下,考虑使用模式总是好的。 然后创建一个类来保存位图,下面是一个草图:
// Add singleton pattern to this class
class MyBitmaps
{
// populate the list somewhere
public List<Bitmap> bitmaps {get; set;}
// Or even use a dictionary where the key is a name or another identifier
public Dictionary<string, Bitmap> {get;set;}
}
//将单例模式添加到此类
类MyBitmap
{
//在某个地方填充列表
公共列表位图{get;set;}
//或者甚至使用字典,其中键是名称或其他标识符
公共字典{get;set;}
}
将类设为静态或类的实例为静态。