Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7550703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:16:56+00:00 2026-05-30T10:16:56+00:00

I have few images embedded in my executable in resource section. I followed these

  • 0

I have few images embedded in my executable in resource section.
I followed these steps to create my executable:

  1. Generated .resx file for all the images (.jpg) in a directory using some utility. The images are named image1.jpg, image2.jpg and so on.
  2. created .resources file from .resx file using: resgen myResource.resx
  3. Embedded the generated .resource file using /res flag as: csc file.cs /res:myResource.resources

4 I am accessing these images as:

ResourceManager resources = new ResourceManager("myResource", Assembly.GetExecutingAssembly());

Image foo = (System.Drawing.Image)(resources.GetObject("image1"));

This all is working fine as expected. Now I want to change embedded images to some new images. This is what I am currently doing:

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());
        }
    }
}

The above code is adding a new resource for the specified image (inside IMAGE title with some random number, as seen in Resource hacker), but I want to modify the existing resource data for image1.

I am sure that I am calling UpdateResource with some invalid arguments.

Could any one help pointing that out?

I am using .NET version 2

Thank you,

Vikram

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:16:57+00:00Added an answer on May 30, 2026 at 10:16 am

    I think you are making a confusion between .NET resources, and Win32 resources. The resources you add embedding with the /res argument to csc.exe are .NET resources that you can successfully read using you ResourceManager snippet code.

    Win32 resources are another beast, that is not much “compatible” with the managed .NET world in general, athough you can indeed add them to a .NET exe using the /win32Res argument – note the subtle difference 🙂

    Now, if you want to modify embedded .NET resources, I don’t think there are classes to do it in the framework itself, however you can use the Mono.Cecil library instead. There is an example that demonstrates this here: C# – How to edit resource of an assembly?

    And if you want to modify embedded Win32 resources, you code needs some fixes, here is a slightly modified version of it, the most important difference lies in the declaration of UpdateResource:

        [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, short wLanguage, byte[] lpData, int cbData);
    
        [DllImport("kernel32.dll")]
        static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);
    
        public static void Main(string[] args)
        {
            IntPtr handle = BeginUpdateResource(args[0], false);
            if (handle == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error()); // this will automatically throw an error with the appropriate human readable message
    
            try
            {
                byte[] imgData = File.ReadAllBytes("SetupImage1.jpg");
    
                if (!UpdateResource(handle, "Image", "image1", (short)CultureInfo.CurrentUICulture.LCID, imgData, imgData.Length))
                    throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            finally
            {
                EndUpdateResource(handle, false);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder with a few images, these images are added by user
I'm using flex and have a few images that I need to sequence through.
I have a fair few images that I'm loading into a ListBox in my
I have to transform a few hundred images every day in a specific way.
I've seen a few fixes for allowing PNG images to have transparency in Internet
I have a page with a few fields and a runtime-generated image on it.
I have few question in this regard When you create an internet page, does
I have few images in a grid, then when i click a button, a
I'm using Delphi 2006. I have a few PNG images with transparencies and I
I have a C# WinForm application that has a few images on it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.