yungbol Posted March 16, 2009 Report Posted March 16, 2009 (edited) 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: 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.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. 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, 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 October 20, 2009 by iBotPeaches Fixed Images.
Korupt Data Posted March 16, 2009 Report Posted March 16, 2009 Keep on posting these tuts Yung, im going to try to get into coding if i have anytime =/ lol
Random1225 Posted March 16, 2009 Report Posted March 16, 2009 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.
yungbol Posted March 16, 2009 Author Report Posted March 16, 2009 (edited) 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 March 16, 2009 by yungbol
iKhaosmaster Posted March 16, 2009 Report Posted March 16, 2009 Thanks for the tutorial yung. It's a great help.
Random1225 Posted March 16, 2009 Report Posted March 16, 2009 I'm pretty sure I did. It looks like this. http://i287.photobucket.com/albums/ll130/Random1225_photos/Untitled-6.png
yungbol Posted March 16, 2009 Author Report Posted March 16, 2009 (edited) Ok, lol I just installed VB.NET and made it no problem. Here is the code for VB.NET: 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) 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 March 16, 2009 by yungbol
gabe_k Posted March 17, 2009 Report Posted March 17, 2009 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
yungbol Posted March 17, 2009 Author Report Posted March 17, 2009 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.
ELEMENT18592 Posted May 7, 2009 Report Posted May 7, 2009 I got about this far then i started getting some errors http://i211.photobucket.com/albums/bb95/element18592/Yungbolhelp.jpg then i went a little bit further and got all this http://i211.photobucket.com/albums/bb95/element18592/yungbolhelp2.jpg Please tell me, what am i doing wrong??? 1
iBotPeaches Posted May 7, 2009 Report Posted May 7, 2009 Where you dim the openfiledialog make sure the () is touching OpenFileDialog like new OpenFileDialog();
yungbol Posted May 7, 2009 Author Report Posted May 7, 2009 (edited) 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 May 7, 2009 by yungbol
ELEMENT18592 Posted May 7, 2009 Report Posted May 7, 2009 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 1
Dark Slipstream Posted May 10, 2009 Report Posted May 10, 2009 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.
yungbol Posted May 10, 2009 Author Report Posted May 10, 2009 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.
ELEMENT18592 Posted May 10, 2009 Report Posted May 10, 2009 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? http://i211.photobucket.com/albums/bb95/element18592/Inject.jpg http://i211.photobucket.com/albums/bb95/element18592/ErrorList.jpg 1
iBotPeaches Posted May 10, 2009 Report Posted May 10, 2009 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
yungbol Posted May 10, 2009 Author Report Posted May 10, 2009 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.
ELEMENT18592 Posted May 10, 2009 Report Posted May 10, 2009 thanks, the only problem is the ending now...this isnt vb so i cant just put end sub....what should i do now? http://i211.photobucket.com/albums/bb95/element18592/onlyproblemnow.jpg 1
EsjayIzBack Posted May 10, 2009 Report Posted May 10, 2009 have you tried building? press F6 and it will tell you the error
Dark Slipstream Posted May 11, 2009 Report Posted May 11, 2009 put it on the next line. if (statement){ }else{ }
Rogue Modder Posted October 20, 2009 Report Posted October 20, 2009 First few images have fucked up. But i saw it before, good tutorial.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now