将 24 位 .ico 保存到文件 - 与调试器一起使用,并非没有
Saving 24bit .ico to a file - Works with Debugger, NOT without
我在尝试将真实的 24bpp ico 保存到文件时遇到未知问题。
我正在使用我在此处找到的 IconEx.dll:Vb icon thingy project
我将您可以看到的原始 VB 代码重写为 C#(下一个)。
我的问题很奇怪。当 运行 Debug/Release 版本使用 F5 或手动将调试器附加到 .exe 时(在开头添加一个 sleep() 以便我能够轻松附加调试器),一切正常!
当我 运行 .exe(发布或调试)时,空的临时(黑色).ico 写入成功,但最终的 ico 只是损坏了......
您是否看到任何常见问题?我已经尝试了很多东西来解决这个问题 4 天了......甚至把 sleep() 放在各处以减慢进程等
两个图标(正确的一个或损坏的一个)具有相同的大小至少 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();
我的午休时间专门用于此,请在控制台应用程序中尝试以下操作,您需要添加适当的引用。在任何模式下(调试、发布、命令提示符等)都表现出色。
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");
}
}
}
}
}
我在尝试将真实的 24bpp ico 保存到文件时遇到未知问题。
我正在使用我在此处找到的 IconEx.dll:Vb icon thingy project
我将您可以看到的原始 VB 代码重写为 C#(下一个)。
我的问题很奇怪。当 运行 Debug/Release 版本使用 F5 或手动将调试器附加到 .exe 时(在开头添加一个 sleep() 以便我能够轻松附加调试器),一切正常!
当我 运行 .exe(发布或调试)时,空的临时(黑色).ico 写入成功,但最终的 ico 只是损坏了...... 您是否看到任何常见问题?我已经尝试了很多东西来解决这个问题 4 天了......甚至把 sleep() 放在各处以减慢进程等
两个图标(正确的一个或损坏的一个)具有相同的大小至少 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();
我的午休时间专门用于此,请在控制台应用程序中尝试以下操作,您需要添加适当的引用。在任何模式下(调试、发布、命令提示符等)都表现出色。
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");
}
}
}
}
}