【发布时间】:2012-03-10 02:58:26
【问题描述】:
我在资源部分的可执行文件中嵌入了很少的图像。 我按照以下步骤创建了我的可执行文件:
- 使用某些实用程序为目录中的所有图像 (.jpg) 生成 .resx 文件。图像命名为 image1.jpg、image2.jpg 等。
- 从 .resx 文件创建 .resources 文件,使用:
resgen myResource.resx - 使用 /res 标志嵌入生成的 .resource 文件为:
csc file.cs /res:myResource.resources
4 我正在访问这些图像:
ResourceManager resources = new ResourceManager("myResource", Assembly.GetExecutingAssembly());
Image foo = (System.Drawing.Image)(resources.GetObject("image1"));
这一切都按预期正常工作。现在我想将嵌入图像更改为一些新图像。这就是我目前正在做的事情:
class foo
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr BeginUpdateResource(string pFileName, bool bDeleteExistingResources);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, string wLanguage, Byte[] lpData, uint cbData);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
public static void Main(string[] args)
{
IntPtr handle = BeginUpdateResource(args[0], false);
if (handle.ToInt32() == 0)
throw new Exception("File Not Found: " + fileName + " last err: " + Marshal.GetLastWin32Error());
byte[] imgData = File.ReadAllBytes("SetupImage1.jpg");
int fileSize = imgData.Length;
Console.WriteLine("Updaing resources");
if (UpdateResource(handle, "Image", "image1", "image1", imgData, (uint)fileSize))
{
EndUpdateResource(handle, false);
Console.WriteLine("Update successfully");
}
else
{
Console.WriteLine("Failed to update resource. err: {0}", Marshal.GetLastWin32Error());
}
}
}
上面的代码是为指定的图片添加一个新的资源(在IMAGE标题里面有一些随机数,见Resource hacker),但是我想修改image1的现有资源数据。
我确定我在调用 UpdateResource 时带有一些无效参数。
谁能帮忙指出来?
我使用的是 .NET 版本 2
谢谢,
维克拉姆
【问题讨论】:
-
两种不同的动物。 .resx 文件包含 托管 资源,它们嵌入到程序集清单中。 UpdateResource() 只知道非托管资源。除了使用 ildasm.exe 反编译和使用 ilasm.exe 重新编译之外,没有任何机制可以重建程序集清单。非常不切实际,尤其是与简单地将 jpegs 文件存储在目录中相比。