在Winform中c#导出所有资源图片

背景:

有时,我们需要从现有Winform程序中得到程序中的相关图片。

但是,有些项目中,图片嵌入到资源文件中,但在资源浏览器中无法查看到相关图片,这时,就需要用类似本程序来导出所有资源图片了。

关键代码:

        string ImagePath = @"images";
        public Dictionary<string, object> Resources { get; private set; }
        private CultureInfo _culture;
        ComponentResourceManager _resourceManager;

private void btnGetAllPng_Click(object sender, EventArgs e)
        {
            _resourceManager = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            Resources = new Dictionary<string, object>();
            _culture = CultureInfo.CurrentUICulture;
            LoadAndSaveBitmapAsPng();
        }

        /// <summary>
        /// Determines whether the specified data is zip.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns><c>true</c> if the specified data is zip; otherwise, <c>false</c>.</returns>
        private bool IsZIP(byte[] data)
        {
            if (data[0] == 0x50 && data[1] == 0x4b && data[2] == 0x03 && data[3] == 0x04)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Loads this instance.
        /// </summary>
        private void LoadAndSaveBitmapAsPng()
        {
            var resourceSet = _resourceManager.GetResourceSet(_culture, true, true);
            int i = 0;
            foreach (DictionaryEntry entry in resourceSet)
            {
                string resourceKey = entry.Key.ToString();
                if (entry.Value is byte[] data)
                {
                    if (IsZIP(data))
                    {
                        LoadZIP(resourceKey, data);
                    }
                    else
                    {
                        Resources.Add(resourceKey, data);
                    }
                }
                else
                {
                    Resources.Add(resourceKey, entry.Value);
                    if (entry.Value is Bitmap)
                    {
                        Bitmap b = entry.Value as Bitmap;
                        string imgPath = Path.Combine(Application.StartupPath, this.ImagePath);
                        if (!Directory.Exists(imgPath))
                            Directory.CreateDirectory(imgPath);
                        b.Save(Path.Combine(Application.StartupPath, this.ImagePath+@"\" + i + ".png"), System.Drawing.Imaging.ImageFormat.Png);
                        i++;
                    }
                }
            }
        }

        /// <summary>
        /// Loads the zip.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="data">The data.</param>
        private void LoadZIP(string name, byte[] data)
        {
            try
            {
                using (var zipMemoryStream = new MemoryStream(data))
                {
                    using (var zip = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
                    {
                        foreach (var entry in zip.Entries)
                        {
                            using (var stream = entry.Open())
                            {
                                using (var ms = new MemoryStream())
                                {
                                    stream.CopyTo(ms);
                                    ms.Position = 0; // rewind
                                    var entryData = new byte[ms.Length];
                                    ms.Read(entryData, 0, entryData.Length);

                                    Resources.Add(entry.Name, entryData);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Debug.WriteLine($"Unable to load ZIP resource file: {name}");
            }
        }

        /// <summary>
        /// Gets the text value.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>System.String.</returns>
        public string GetTextValue(string name)
        {
            if (!Resources.ContainsKey(name))
            {
                return null;
            }

            if (!(Resources[name] is byte[]))
            {
                return null;
            }

            return System.Text.Encoding.UTF8.GetString((byte[])Resources[name]);
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值