* UIImageListBox: 从文件载入图片,并且解除占用

This commit is contained in:
Sunny 2021-08-07 10:37:13 +08:00
parent 8cce9a417e
commit e1823a882b
8 changed files with 29 additions and 2 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -17,6 +17,13 @@
{ {
components.Dispose(); components.Dispose();
} }
foreach (var item in Items)
{
if (item is ImageListItem imageItem)
imageItem.Dispose();
}
base.Dispose(disposing); base.Dispose(disposing);
} }

View File

@ -727,14 +727,14 @@ namespace Sunny.UI
public string Description { get; set; } public string Description { get; set; }
public Bitmap Image { get; private set; } public Image Image { get; private set; }
public ImageListItem(string imagePath, string description = "") public ImageListItem(string imagePath, string description = "")
{ {
if (File.Exists(imagePath)) if (File.Exists(imagePath))
{ {
ImagePath = imagePath; ImagePath = imagePath;
Image = new Bitmap(imagePath); Image = ImageEx.FromFile(imagePath);
} }
Description = description; Description = description;

View File

@ -84,6 +84,26 @@ namespace Sunny.UI
return sb.ToString(); return sb.ToString();
} }
public static Image FromFile(string path)
{
if (File.Exists(path))
{
try
{
byte[] bytes = File.ReadAllBytes(path);
return System.Drawing.Image.FromStream(new MemoryStream(bytes));
}
catch (Exception)
{
return null;
}
}
else
{
return null;
}
}
public static Color RandomColor() public static Color RandomColor()
{ {
Random random = new Random(); Random random = new Random();