我在尝试将真正的24bpp ico保存到文件时遇到了一个未知问题。
我使用的是我在这里找到的IconEx.dll:Vb icon thingy project
我将原来的VB代码改写成C#(后面的代码)。
我的问题很奇怪。当使用F5运行Debug / Release版本或手动将调试器附加到.exe(在开头为我添加sleep()以便能够轻松附加调试器)时,一切正常!
当我刚刚运行.exe(Release或Debug)时,空的临时(黑色).ico成功写入,但最终的ico刚刚被破坏... 你看到任何常见的问题吗?我已经尝试了很多东西来解决这个问题,现在已经4天了......甚至可以把睡眠()放到各处以减缓这个过程的速度等等。
两个图标(正确的图标或已损坏的图标)具有相同的尺寸,至少为9.43Ko
谢谢。
//create a temporary icon from a Bmp
Bitmap nwbmp = new Bitmap(48, 48, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
IntPtr pntr = nwbmp.GetHicon();
Icon nwico = Icon.FromHandle(pntr);
DestroyIcon(pntr);
using (System.IO.Stream st = new System.IO.FileStream(pathToFinaleIco, FileMode.Create)) {
System.IO.BinaryWriter wr = new System.IO.BinaryWriter(st);
nwico.Save(st);
wr.Close();
}
nwbmp.Dispose();
//create the final icon by writing in the temp one and then saving to hdd overwritting to it
Bitmap bmp = new Bitmap("path.to.file.bmp", new Size(48, 48)); //<== takes the bmp i want the ico to looks like
IconEx Iconex = new IconEx(pathToFinaleIco); //<=== load the temp ico file ill overwrite to be final one
Iconex.Items.RemoveAt(0);
IconDeviceImage IcondeviceImage = new IconDeviceImage(new Size(48, 48), ColorDepth.Depth32Bit);
IcondeviceImage.IconImage = new Bitmap(bmp);
Iconex.Items.Add(IcondeviceImage);
Iconex.Save(pathToFinaleIco);
//end
bmp.Dispose();
答案 0 :(得分:1)
致力于我的午休时间,在控制台应用程序中尝试以下内容,您需要添加适当的引用。在任何模式下都能很好地工作(调试,发布,命令提示等)。
Program.cs的
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace BmpMadness
{
class Program
{
static void Main(string[] args)
{
using (Image bmp = Image.FromFile("target.bmp"))
using (Bitmap newBmp = new Bitmap(bmp, new Size(48, 48)))
using (Bitmap newFormatBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format24bppRgb))
{
// DestroyIcon(pntr); - dont need it.
using (System.IO.Stream st = new System.IO.FileStream("final.ico", FileMode.Create))
{
IntPtr pntr = newFormatBmp.GetHicon();
Icon nwico = Icon.FromHandle(pntr);
System.IO.BinaryWriter wr = new System.IO.BinaryWriter(st);
nwico.Save(st);
}
//create the final icon by writing in the temp one and then saving to hdd overwritting to it
using (var Iconex = new IconEx("final.ico"))
{
Iconex.Items.RemoveAt(0);
IconDeviceImage IcondeviceImage = new IconDeviceImage(new Size(48, 48), ColorDepth.Depth32Bit);
IcondeviceImage.IconImage = new Bitmap(bmp);
Iconex.Items.Add(IcondeviceImage);
Iconex.Save("deviceImage.ico");
}
}
}
}
}