I'll be honest and say that I don't know if this is going to be that useful to anyone out there, but hey, it's free code. Anyway, I recently ran into a situation where I needed to get the palette that was used by an image, take that palette and convert it to HTML based colors (#012345, etc.). It's actually very simple but unless you know about the ColorTranslator class, you'd be scratching your head as to how to do it in a simple manner. Luckily for us that class exists and contains a function called ToHTML which converts a color object to an HTML based string. For our purposes, we're just going to do the following:
public static List<string> GetHTMLPalette(Bitmap Image)
{
List<string> ReturnArray = new List<string>();
if (Image.Palette != null && Image.Palette.Entries.Length > 0)
{
for (int x = 0; x < Image.Palette.Entries.Length; ++x)
{
string TempColor = ColorTranslator.ToHtml(Image.Palette.Entries[x]);
if (!ReturnArray.Contains(TempColor))
{
ReturnArray.Add(TempColor);
}
}
return ReturnArray;
}
for (int x = 0; x < Image.Width; ++x)
{
for (int y = 0; y < Image.Height; ++y)
{
string TempColor = ColorTranslator.ToHtml(Image.GetPixel(x, y));
if (!ReturnArray.Contains(TempColor))
{
ReturnArray.Add(TempColor);
}
}
}
return ReturnArray;
}
That's it. All the code does, is checks if there is a palette. If there is, it simply copies those values (once converted) to the return array. Otherwise it goes through each pixel and converts it that way, building up the array as it goes. Very simple and gets the job done. Once again, I don't know if this code is going to help anyone out there, but you never know. Anyway, try it out, leave feedback, and happy coding.
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
I've been programming for a while, not as long as some out there, but for a while. I have programs/code that I've written that has spanned from QBasic to C to Java to, well, you name it really. With each language you gain some things and lose some things. And if you switch platforms, you're definitely getting different libraries, etc... Now that I'm primarily on C#, I have to deal with the lose of a decent built in INI file reader/writer. In C++ (assuming you're on windows), you just call GetPrivateProfileString and I was good to go. In .Net, there's nothing.
It makes perfect sense that there wouldn't be anything in .Net for reading from or writing to an INI file:
-
They want you to use XML (which you should be doing)
-
Use XML already!
But you may run into situations where you need to read an INI file to move over settings. In these instances you're going to have to read it in yourself... Or use this class that I've created... The choice is yours really:
INI.zip (2.07 kb)
I've commented the class. All it really does is parses the file, finding each section, key, and value and holds it in a Dictionary object (which contains the section header and another dictionary containing the key/value pairs). It even writes the file out if you need or puts it into an XML format if you'd like to parse that instead. Anyway, I hope this helps out someone. So give it a try, leave feedback, and happy coding.
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
This is actually the same code that I used for the BlogEngine.Net extension that I created a while back. However I needed it to be a bit more generic (not BlogEngine specific) and take the code from a text box so I ended up creating an AJAX extender:
CopyToClipboardExtender.zip (2.07 kb)
It's rather basic. The targetID is the link you want them to click on to copy the text. The CopyID is the text box's ID. It most likely will not work in Firefox, etc. and is really only IE specific. However it's better than nothing.
As far as setting it up, sometimes I get a bit lazy when it comes to explaining how to use some of the code on here. In the case of the AJAX controls, I usually leave out the fact that you need to download the AJAX Control Toolkit. Once you have that and you've added the templates, etc. like it says in the setup, you need to create an ASP.Net AJAX Control Project (I've used the name of AJAXControls, so if you use something different you'll need to potentially change some of the code to point to the correct namespace). From there, you add the code (making sure to have the js file as an embedded resource). Compile and you're ready to go...
Now that jQuery is going to be the norm, the setup process will probably change in the future. Plus I plan on packaging these up at some point like the utilities. For now though I leave you with the code. So try it out, leave feedback, and happy coding.
Be the first to rate this post - Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
|
|