C# winform imageList1添加图片后 程序关闭后就没了

大飞为 2015-07-08 02:39:38
file = this.openFileDialog1.FileName;
Image a = Image.FromFile(file) ;
imageList1.Images.Add(a);



程序重启 imageList1里面就没了 Image a ;
我想保存起来
...全文
224 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
大飞为 2015-07-08
  • 打赏
  • 举报
回复
引用 18 楼 Z65443344 的回复:
或者如果你的imageList是代码写死的 那么其实你只需要将int型的索引保存到文件,下次读取出来,就行了
现在是这样的imageList的图片是读取img文件夹的 里面有个aa.png 我用System.IO.File.Copy复制到里面去 System.IO.File.Copy不是用个覆盖效果的么 但是并不覆盖
於黾 2015-07-08
  • 打赏
  • 举报
回复
或者如果你的imageList是代码写死的 那么其实你只需要将int型的索引保存到文件,下次读取出来,就行了
於黾 2015-07-08
  • 打赏
  • 举报
回复
如果你imageList里的图片本身就都是从文件里来的 那么你直接弄个txt保存当前选择的图片的路径,下次读取的时候从那个文件读取就行了
於黾 2015-07-08
  • 打赏
  • 举报
回复
System.IO.File.Copy是复制文件用的 就是从硬盘里将一个文件复制到另一个目录下 你现在的image在内存里,并不在文件里 所以你应该调用image的save方法保存到文件 再次打开的时候调用image.FromFile方法读取文件
大飞为 2015-07-08
  • 打赏
  • 举报
回复
引用 13 楼 Z65443344 的回复:
System.IO.File.Copy 你想复制啥玩意啊?? 你得将image保存到文件 再次打开的时候从文件读取
就是说那个文件夹里面有同名文件但并不覆盖
大飞为 2015-07-08
  • 打赏
  • 举报
回复
引用 13 楼 Z65443344 的回复:
System.IO.File.Copy 你想复制啥玩意啊?? 你得将image保存到文件 再次打开的时候从文件读取
是的
於黾 2015-07-08
  • 打赏
  • 举报
回复
System.IO.File.Copy 你想复制啥玩意啊?? 你得将image保存到文件 再次打开的时候从文件读取
exception92 2015-07-08
  • 打赏
  • 举报
回复
引用 6 楼 starfd 的回复:
你是windows程序,用AppDomain.CurrentDomain.BaseDirectory来获取你的exe所在物理路径(文件夹),加你的相对路径组成一个完整的路径
看10楼。我也是醉了,,,,,
exception92 2015-07-08
  • 打赏
  • 举报
回复
引用 10 楼 u014138682 的回复:
[quote=引用 9 楼 duanzi_peng 的回复:] [quote=引用 7 楼 u014138682 的回复:] [quote=引用 5 楼 duanzi_peng 的回复:] [quote=引用 3 楼 u014138682 的回复:] @duanzi_peng 为什么他说我的相对路径错误呢
那个路径是你的应用程序的运行路径,也就是你的exe 的路径。不是简单的 \\img 。[/quote] System.IO.File.Copy(file, "..\\..\\img\\" + name, true);为什么我这里没有覆效果呢 我程序打包后 我现在对了路径打包后会报错么[/quote] 6楼都给你说了路径的写法,,[/quote] 看不懂他说什么 System.IO.File.Copy(file, "..\\..\\img\\" + name, true);为什么我这里没有覆效果呢[/quote] ..\\..\\img\\ 你这是BS的写法。6楼已经给你解释过了。 看不懂就别解决了。
大飞为 2015-07-08
  • 打赏
  • 举报
回复
引用 9 楼 duanzi_peng 的回复:
[quote=引用 7 楼 u014138682 的回复:] [quote=引用 5 楼 duanzi_peng 的回复:] [quote=引用 3 楼 u014138682 的回复:] @duanzi_peng 为什么他说我的相对路径错误呢
那个路径是你的应用程序的运行路径,也就是你的exe 的路径。不是简单的 \\img 。[/quote] System.IO.File.Copy(file, "..\\..\\img\\" + name, true);为什么我这里没有覆效果呢 我程序打包后 我现在对了路径打包后会报错么[/quote] 6楼都给你说了路径的写法,,[/quote] 看不懂他说什么 System.IO.File.Copy(file, "..\\..\\img\\" + name, true);为什么我这里没有覆效果呢
exception92 2015-07-08
  • 打赏
  • 举报
回复
引用 7 楼 u014138682 的回复:
[quote=引用 5 楼 duanzi_peng 的回复:] [quote=引用 3 楼 u014138682 的回复:] @duanzi_peng 为什么他说我的相对路径错误呢
那个路径是你的应用程序的运行路径,也就是你的exe 的路径。不是简单的 \\img 。[/quote] System.IO.File.Copy(file, "..\\..\\img\\" + name, true);为什么我这里没有覆效果呢 我程序打包后 我现在对了路径打包后会报错么[/quote] 6楼都给你说了路径的写法,,
大飞为 2015-07-08
  • 打赏
  • 举报
回复
引用 6 楼 starfd 的回复:
你是windows程序,用AppDomain.CurrentDomain.BaseDirectory来获取你的exe所在物理路径(文件夹),加你的相对路径组成一个完整的路径
System.IO.File.Copy(file, "..\\..\\img\\" + name, true);为什么我这里没有覆效果呢 我程序打包后 我现在对了路径打包后会报错么
大飞为 2015-07-08
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
[quote=引用 3 楼 u014138682 的回复:] @duanzi_peng 为什么他说我的相对路径错误呢
那个路径是你的应用程序的运行路径,也就是你的exe 的路径。不是简单的 \\img 。[/quote] System.IO.File.Copy(file, "..\\..\\img\\" + name, true);为什么我这里没有覆效果呢 我程序打包后 我现在对了路径打包后会报错么
  • 打赏
  • 举报
回复
你是windows程序,用AppDomain.CurrentDomain.BaseDirectory来获取你的exe所在物理路径(文件夹),加你的相对路径组成一个完整的路径
exception92 2015-07-08
  • 打赏
  • 举报
回复
引用 3 楼 u014138682 的回复:
@duanzi_peng 为什么他说我的相对路径错误呢
那个路径是你的应用程序的运行路径,也就是你的exe 的路径。不是简单的 \\img 。
大飞为 2015-07-08
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
imageList1.Images.Add(a);之后,在你的项目根目录下 创建一个用于img的文件夹,将 a 复制到Img文件中,下次加载读取的时候 直接读取这个img下的图片就行了。



为什么他说我的相对路径错误呢
大飞为 2015-07-08
  • 打赏
  • 举报
回复
@duanzi_peng 为什么他说我的相对路径错误呢
  • 打赏
  • 举报
回复
关闭之前将imageList1里面的图片保存为文件,下次启动的时候从保存路径去加载
exception92 2015-07-08
  • 打赏
  • 举报
回复
imageList1.Images.Add(a);之后,在你的项目根目录下 创建一个用于img的文件夹,将 a 复制到Img文件中,下次加载读取的时候 直接读取这个img下的图片就行了。
这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndex、ImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧