Hack Forums
[.NET] ImageToCode (and CodeToImage): Embed images in class files! - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] ImageToCode (and CodeToImage): Embed images in class files! (/showthread.php?tid=2576637)

Pages: Pages 1 2 3


ImageToCode (and CodeToImage): Embed images in class files! - Meerkat. - Jun 11, 2012

Hello everyone. I've always wanted to embed small images in my themes (icons, logo's, whatever) but I hate hardcoding resource names in my classes.
Of course you can give the user some .png files and let them add them to the resources, but I wanted something better :)

This class can convert images into code (suitable for Visual Studio, I'll explain that later) and convert them back into images.
This way you can embed little icons in your class file!

Visual studio has a maximum line length of 65530 (or something like that). To prevent errors from happening I've added an option to output a VS-compatible string.

To use this, just add the class and use this code:

Convert an image into code:
Code
Dim ITC As New ImageToCodeClass
Dim Code as String =  ITC.ImageToCode(YourImage)

Convert the code into an image:
Code
Dim ITC As New ImageToCodeClass
PictureBox1.Image = ITC.CodeToImage("<YOUR IMAGE CODE HERE>")



Download:
Spoiler (Click to View)

Note: This isn't supposed to be top-notch coding. It's made to work, not made to be 100% perfect and professional. I made it for personal purposes and just decided to share this with you guys.
EDIT: Completely new class, super small yet much better. Better performance, smaller string,... Thanks for the suggestions!


* - aTastyUnicorn - Jun 11, 2012

saw this on youtube, really nice work

keep up mate


* - DotNetNut - Jun 11, 2012

Where did you find out about these image codes? Never herd of them before and still not really sure why this is better?

I mean nice app and video but how? , why? lol :P


* - Deceptive - Jun 11, 2012

You're just getting each pixel, and logging their RGB right? :3


* - DotNetNut - Jun 11, 2012

(Jun 11, 2012 12:59 PM)Deceptive Wrote:  You're just getting each pixel, and logging their RGB right? :3

Yea thats what I got from the pastebin link, But why? whats the benefit?

I mean why not into a byte array or something?


* - Deceptive - Jun 11, 2012

(Jun 11, 2012 01:02 PM)DotNetNut Wrote:  Yea thats what I got from the pastebin link, But why? whats the benefit?

I mean why not into a byte array or something?

Evidently so he doesn't have to store his images in resources.


* - DotNetNut - Jun 11, 2012

(Jun 11, 2012 01:12 PM)Deceptive Wrote:  Evidently so he doesn't have to store his images in resources.

Oh.. ok lol, I thought it would of been easier to convert from Image > Bytearray > string?

I take it doing it that way, as i suggested above would make it a massive sting.. And also i wonder why he dose not want to store in resources? I mean is that not what their for?

I guess i just want to know if this method is actually better or ? lol.

confused... :P


* - Meerkat. - Jun 11, 2012

(Jun 11, 2012 01:12 PM)Deceptive Wrote:  Evidently so he doesn't have to store his images in resources.

Exactly. I want to include small images into themes and still be able to just post a pastebin link.
There's probably lots of better ways to do this, and my method with splitting things probably isn't the most efficient, but it works :D


* - DotNetNut - Jun 11, 2012

(Jun 11, 2012 01:20 PM)Mavamaarten~ Wrote:  Exactly. I want to include small images into themes and still be able to just post a pastebin link.
There's probably lots of better ways to do this, and my method with splitting things probably isn't the most efficient, but it works :D

Right i got you know :D lol, I thought this was just a way for you to store them when making new apps lulz, But i can see the benefit of it now :P


* - DotNetNut - Jun 11, 2012

I just tried a few of these and omg the string are fucking huge!

I tried to post one string as an example but it was apparently 16300 chars over the limit on a 60kb image lul..

EDIT : And i see you have gone to the dark side.. Enjoy your uber :D


* - Support. - Jun 12, 2012

Ahhh, another good release, will definitly have to have a play with this!


* - marvinn - Jun 12, 2012

Why not just using a binarySeralizer and seralize it to a byte array and convert that to a base64 string.. I think it could take like 5 lines of code..

Anyways, gj


* - Αndromeda - Jun 12, 2012

I can't call this Stenography and still. This is amazing. Thank you.


* - lolcakes131 - Jun 12, 2012

(Jun 12, 2012 03:40 AM)marvinn Wrote:  Why not just using a binarySeralizer and seralize it to a byte array and convert that to a base64 string.. I think it could take like 5 lines of code..

Anyways, gj

This is what I'd suggest too (perhaps not serializing, but just converting the image to it's byte representation and then to a Base64 string), it's how resources in Visual Studio are stored in RESX files. Iterating through each pixel isn't very efficient and uses a lot of resources, not to mention the string this produces can be rediculously large, but then again, you did explictly mention "this isn't supposed to be top-notch coding".


* - Washi - Jun 12, 2012

I like the idea, but the strings become loooonng..
Maybe you can take a look at compressing the strings with a GZip or Deflate Stream.