Jump to content

  •  

  • iBotModz CBox


    Photo

    [C# tutorial] Injecting Images


    • Please log in to reply
    24 replies to this topic

    #1 yungbol

    yungbol

      Firmware Expert

    • Global Mods

    • 1,461 posts
    • Joined: 09-April 08
    • Gender:Male
    • Location:Philly

    Posted 16 March 2009 - 12:01 PM

    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.
    Ok, so open C# create a new windows app and name it whatever you want. In this case I will be naming it iBotModz.

    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:

    tut1.png

    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:

    tut2.png

    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:

    tut3.png

    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:

    tut4.png

    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.
    Now, debug you app and load a CON file. It should work flawlessly. Now we are ready to move on to the next part which is extracting or saving the image.

    Go double click your "extract button" so that you can enter code for it.

    tut5.png

    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:

    tut6.png

    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, that is really hard and advanced, but if you get that down, you are set to move on. That was something I just learned and it can probably be done many different ways, so if someone has another way, let me know.

    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.


    #2 Korupt Data

    Korupt Data

      SoOoO KoRuPtAbLe

    • Retired Staff

    • 3,503 posts
    • Joined: 12-May 08
    • Gender:Male
    • Location:That file you downloaded

    Posted 16 March 2009 - 12:46 PM

    Keep on posting these tuts Yung, im going to try to get into coding if i have anytime =/ lol

    #3 Random1225

    Random1225

      Gunnery Sergeant Grade 2

    • VIP

    • 986 posts
    • Joined: 03-May 08
    • Gender:Male
    • Location:The Holy Empire of Britannia

    Posted 16 March 2009 - 12:51 PM

    Nice tut keep it up. Also I'm more used to VB so I tried writing this in Vb and I have this one error. It says "Image.FromStream" is not a member of System.IO so I tried writing it differently and it still didn't work so I tried converting it using a Vb to C# converter and It still didn't work. I know your more into C#, but I thought I'd ask if you knew whats wrong.

    #4 yungbol

    yungbol

      Firmware Expert

    • Global Mods

    • 1,461 posts
    • Joined: 09-April 08
    • Gender:Male
    • Location:Philly

    Posted 16 March 2009 - 12:54 PM

    Are you declaring your Stream properly?

    Dim image As Stream = New MemoryStream(location)
    pictureBox1.Image = Image.FromStream(image)

    ^ That is VB.NET, anyone following this tut don't pay attention to that reply lol.

    Edited by yungbol, 16 March 2009 - 12:55 PM.


    #5 iKhaosmaster

    iKhaosmaster

      Yawn

    • Members+

    • 1,346 posts
    • Joined: 29-April 08
    • Gender:Male

    Posted 16 March 2009 - 01:18 PM

    Thanks for the tutorial yung. It's a great help.

    #6 Random1225

    Random1225

      Gunnery Sergeant Grade 2

    • VIP

    • 986 posts
    • Joined: 03-May 08
    • Gender:Male
    • Location:The Holy Empire of Britannia

    Posted 16 March 2009 - 01:18 PM

    I'm pretty sure I did. It looks like this.

    Posted Image

    #7 yungbol

    yungbol

      Firmware Expert

    • Global Mods

    • 1,461 posts
    • Joined: 09-April 08
    • Gender:Male
    • Location:Philly

    Posted 16 March 2009 - 01:47 PM

    Ok, lol I just installed VB.NET and made it no problem. Here is the code for VB.NET:

    [codebox]Dim fs As New FileStream(TextBox1.Text, FileMode.Open)
    Dim br As New BinaryReader(fs)
    br.BaseStream.Position = &H171A
    Dim imaged As New MemoryStream(br.ReadBytes(16000))
    PictureBox1.Image = Image.FromStream(imaged)[/codebox]

    vb.net gets confused here and there... another reason I use c#.

    The reason why I changed it to imaged was because when you put image it would get confused from the one with the capital I. C# is case-sensitive, that is why it was different. You also need to declare the MemoryStream as it's own, not as a Stream.

    Edited by yungbol, 16 March 2009 - 01:50 PM.


    #8 Random1225

    Random1225

      Gunnery Sergeant Grade 2

    • VIP

    • 986 posts
    • Joined: 03-May 08
    • Gender:Male
    • Location:The Holy Empire of Britannia

    Posted 16 March 2009 - 02:21 PM

    Thanks yungbol that fixed it.

    #9 gabe_k

    gabe_k

      Corporal Grade 2

    • Donors+

    • 360 posts
    • Joined: 02-May 08
    • Gender:Male

    Posted 17 March 2009 - 01:42 PM

    bw.BaseStream.Position = 0x1712;
    bw.Write(imageLength);
    bw.Write(imageLength);

    If you're replacing both PNGs that fixes the lengths I believe (no files or hex editor on this computer ATM :/). Just thought I'd add that :p

    #10 yungbol

    yungbol

      Firmware Expert

    • Global Mods

    • 1,461 posts
    • Joined: 09-April 08
    • Gender:Male
    • Location:Philly

    Posted 17 March 2009 - 02:14 PM

    Yea lol, so it makes it proper to read the image. I don't think it has any effect on the file though... I don't know but in all my image readers I am more lenient and make it read 16000 bytes rather then the int before the PNG.

    #11 gabe_k

    gabe_k

      Corporal Grade 2

    • Donors+

    • 360 posts
    • Joined: 02-May 08
    • Gender:Male

    Posted 17 March 2009 - 05:11 PM

    The 360 just reads what it's told to.

    #12 ELEMENT18592

    ELEMENT18592

      Private Grade 2

    • Members+

    • 56 posts
    • Joined: 08-September 08
    • Gender:Male
    • Location:Mississippi

    Posted 07 May 2009 - 04:08 AM

    I got about this far then i started getting some errors

    Posted Image


    then i went a little bit further and got all this


    Posted Image





    Please tell me, what am i doing wrong???
    • SkateboardxSinner likes this

    #13 iBotPeaches

    iBotPeaches

      General Grade 6

    • Owner

    • 6,570 posts
    • Joined: 29-July 07
    • Gender:Male
    • Location:Kansas

    Posted 07 May 2009 - 06:49 AM

    Where you dim the openfiledialog make sure the () is touching OpenFileDialog

    like new OpenFileDialog();

    #14 yungbol

    yungbol

      Firmware Expert

    • Global Mods

    • 1,461 posts
    • Joined: 09-April 08
    • Gender:Male
    • Location:Philly

    Posted 07 May 2009 - 12:42 PM

    Your problem is letter case. Example:

    Openfiledialog should be OpenFileDialog.

    C# is a case sensitive language.

    Also, I just noticed your brackets are backwards. Your code should look exactly like mines in the first post.

    Edited by yungbol, 07 May 2009 - 12:43 PM.


    #15 ELEMENT18592

    ELEMENT18592

      Private Grade 2

    • Members+

    • 56 posts
    • Joined: 08-September 08
    • Gender:Male
    • Location:Mississippi

    Posted 07 May 2009 - 04:56 PM

    Your problem is letter case. Example:

    Openfiledialog should be OpenFileDialog.

    C# is a case sensitive language.

    Also, I just noticed your brackets are backwards. Your code should look exactly like mines in the first post.


    ok thanks a lot guys
    • SkateboardxSinner likes this

    #16 Dark Slipstream

    Dark Slipstream

      Blue Shadowz Owner

    • Members+

    • 2,829 posts
    • Joined: 19-April 08
    • Gender:Male
    • Location:Canada, ON

    Posted 09 May 2009 - 09:42 PM

    meh, yungbol your source for extracting is kind of off lol.

    you are saying that the image is always 16000 bytes, incorrect lol. Size is different, you should account for that. ;)

    #17 yungbol

    yungbol

      Firmware Expert

    • Global Mods

    • 1,461 posts
    • Joined: 09-April 08
    • Gender:Male
    • Location:Philly

    Posted 09 May 2009 - 09:56 PM

    Dude, you act like I don't know that, I could read the Int32 if I wanted to.. But some programs that inject (Modio) don't fix that Int32 so mines reads the max size.

    #18 ELEMENT18592

    ELEMENT18592

      Private Grade 2

    • Members+

    • 56 posts
    • Joined: 08-September 08
    • Gender:Male
    • Location:Mississippi

    Posted 10 May 2009 - 12:54 AM

    ok i got everthing else to work correctly now im having trouble with the inject part...i just cant seem to get it working...can somebody please point out what i need to do?



    Posted Image



    Posted Image
    • SkateboardxSinner likes this

    #19 iBotPeaches

    iBotPeaches

      General Grade 6

    • Owner

    • 6,570 posts
    • Joined: 29-July 07
    • Gender:Male
    • Location:Kansas

    Posted 10 May 2009 - 05:56 AM

    Make sure their zeros, not O's.

    I think that might be part of it, I could be wrong though. I'm still learning too :)

    #20 yungbol

    yungbol

      Firmware Expert

    • Global Mods

    • 1,461 posts
    • Joined: 09-April 08
    • Gender:Male
    • Location:Philly

    Posted 10 May 2009 - 11:03 AM

    What Peaches said. In hex there are no O's, it is a 0.

    Also, I noticed that you had a "," where it should be a "=".

    And Peaches, everyone is always still learning.