Injecting Images in C#
Ok, so before using this tutorial I recommend you mess around with the language a little bit. Follow my other tutorial on how to show images then, I am sure you will be ready for this.
Note: In this tutorial I will only be showing pictures of the code, I want you to get used to typing it rather then copy and pasting.
Overview of process:
- Open a file.
- Show the image in the container header.
- Add an option to save the image.
- Add an option to inject the image.
Be sure to do our usual steps to the form to make sure no one can resize it or maximize it. Go to tutorial 1 for that.
Now, add 3 buttons and name them "Open", "Extract", and "Inject". Also add a picturebox to your form. It should look like:

Notice we made the picturebox's properties to "stretch" so the image will fill the full box.
Now, double click the "open button" so that the code will appear. Now, instead of using a textbox like last time, we will be using a string. The string will act as an 'invisible' textbox. The code for a string looks like so:

Below it is the OpenFileDialog coding as well. Input that and your code should look exactly like mines.
Ok, now we want to add a class so that when we open a file, the image will automatically load. To input this, we add a 'void'. Enter the void like so:

If you noticed I added that class to the OpenFileDialog result box. That will make this void automatically occur when a file is opened. Now we need to add some code to this void. The code we will add will display the image in the picture box. Just like my previous tutorial we will need to add the IO into the namespace. If you don't know how to do that just enter this at the top:
using System.IO;
Now in our void we will add the following code:

Explanation of code:
- FileStream fs is creating the 'browsing' of the file. It is what opens the file and makes it readable like in a hex editor.
- BinaryReader br is what will read ASCII or hex for you.
- The BaseStream.Position is the offset you will be on.
- Creating the stream is what will read the image from hex and send it to memory so it can be used at a different time in the code.
- Then we load the image from the stream to show it.
Go double click your "extract button" so that you can enter code for it.

Explanation of code - All you are really doing here is creating a SaveFileDialog and saving the image that is in the picturebox. This code is pretty self-explanatory.
Now debug it, at it should now be able to save images. Now, the hard part... Injecting images.
Go double click on your inject button to bring up the code. Just type in this following code and read the explanation after:

Explanation of code:
- The open file dialog is going to allow the user to select the image he/she wants.
- The filestream imageReader is going to be used for reading the image selected.
- The byte[] imageLength is getting the length of the image they select and declaring it as a byte[] (which is basically bytes)
- Then the BinaryReader br is going to read the image.
- The int data is basically getting the length of the file in a decimal or integer rather then in hex data.
- Then we reset the postition to 0.
- Now, we read the whole image. The whole process above gives us a 'read to end' method.
- We then close them streams and open new ones for the container to inject.
- We declare a BinaryWriter and set the position to the image beginning.
- We need to create a loop (while) in order to null out the original image will null bytes. This prevent your image from being smaller and writing and then having all excess data at the end from the old image.
- We then reset the position to the image beginning and write the image.
- We input the showImage class to make the new image display.
- We close our streams.
Now, debug your app and bam! You can now open, extract, and inject images.
Edited by iBotPeaches, 20 October 2009 - 05:54 PM.
Fixed Images.