I needed a method to get the mime type of a System.Drawing Image instance that I'd loaded from an upload. All articles I was able to find online suggest some kind of lookup table solution. There's actually another way. Given an Image i, you can map from the i.RawFormat.Guid property to an ImageCodecInfo.FormatID. So it becomes a very simple for loop to get the MIME type of an image or bitmap. This works for any format System.Drawing/GDI+ supports (bmp, png, tiff, jpg/jpeg, etc). Here's the code:
public static stringGetMimeType(Imagei)
{
foreach (ImageCodecInfo codec inImageCodecInfo.GetImageDecoders())
{
if (codec.FormatID == i.RawFormat.Guid)
return codec.MimeType;
}
return "image/unknown";
}
This won't work for images that were created on the fly (will return "image/unknown"), but will work for all images that were loaded from some source (file, stream, byte[], etc).
Sometimes you want to work with all controls on an ASP.NET page by type rather than id. I finally got fed up with the lack of builtin support, and wrote the following method that returns all controls inside another control (or page) by type:
public List<T> FindControls<T>(Control parent) where <T> : Control
{
List<T> foundControls = new List<T>();
FindControls<T>(parent, foundControls);
return foundControls;
}
void FindControls<T>(Control parent, List<T> foundControls) where <T> : Control
{
foreach (Control c in parent.Controls)
{
if (c is <T>)
foundControls.Add((<T>)c);
else if(c.Controls.Count > 0)
FindControls<T>(parent, foundControls);
}
}
Lutz Roeder's
Reflector is one of the most useful tools out there. It's a disassembler that allows you to point at any assembly and break down the code.
Scott Hansleman shows off a new PowerShell language plugin for Reflector as well as a nice snapshot of the other addins. It's a great read -- I keep forgetting about all the useful plugins you can add to Reflector.
As I develop, I often have images that are bitmaps or other formats that I want to convert to transparent .png's for use in a website. I whipped up a little app that does a bmp2png conversion, using the RegistryHelper class to hook the right click context menu.
Converting an image in .NET is very straightforward, as long as you are doing a conversion from and to a .NET supported image format. Just load the image using the System.Drawing classes, and then save it as the image format you want. Making a color transparent is easy as well -- call the Bitmap.MakeTransparent method and pass in the color to make transparent.
Here's a snippet of the code I used to do the conversion and transparency settings:
using (Stream s = File.OpenRead(srcFileName))
using (Bitmap src = Image.FromStream(s, false, false) as Bitmap)
{
src.MakeTransparent(TransparentColor);
src.Save(destFileName, ImageFormat.Png);
}
The transparent color by default is RGB 0, 255, 128. You can change it using a appSettings key in the app.config as follows:
<add key="transparentColor" value="0, 255, 128" />
The app registers the context menu extension automatically when you run it with no arguments, and thereafter .bmp files will have a "Convert to png" right click context menu item. It would be very simple to add additional from and to formats to this code. Let me know if this is something you'd be interested in and I'll add it.
NOTE: To register the extension, administrative permissions are required.
Bmp2Png executable
Bmp2Png source