Find image format using Bitmap object in C#
If you want to know the format of an image, you can load the file with the Image class, and check its RawFormat property:
using(Image img = Image.FromFile(@"C:\path\to\img.jpg"))
{
if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
{
// ...
}
}
How to get the image format of a BitmapImage?
How can I get the format of the BitmapImage?
You can't, because it does not have a fixed format.
You may use any of the classed derived from BitmapEncoder
to encode a BitmapSource. The format is then determined by the BitmapEncoder that is actually used, e.g. PNG
when you use PngBitmapEncoder
.
How can I know what image format I get from a stream?
You may checkout the Image.RawFormat
property. So once you load the image from the stream you could test:
if (ImageFormat.Jpeg.Equals(image.RawFormat))
{
// JPEG
}
else if (ImageFormat.Png.Equals(image.RawFormat))
{
// PNG
}
else if (ImageFormat.Gif.Equals(image.RawFormat))
{
// GIF
}
... etc
How to retrieve an Image in Bitmap format from the Media Library
I found out eventually; this is the code that will convert an image residing in Sitecore's Media Library in System.Drawing.Bitmap format:
var item = new Sitecore.Context.Database.GetItem(imageId);
var mediaItem = Sitecore.Data.Items.MediaItem(item);
var image = new System.Drawing.Bitmap(mediaItem.GetMediaStream());
how to get exact part of Bitmap object
You can just get a Rectangle as a parameter and look just at the points inside that rectangle, to make sure that all points in rectangle falls inside the bitmap, you need to do Math.Min(bmp.Height, region.Y + region.Height)
and Math.Min(bmp.Width, region.X+Region.Width)
instead of just region.Y + region.Height
and region.X+Region.Width
:
ulong CountPixels(Bitmap bm, Color target_color, Rectangle region)
{
// Loop through the pixels.
ulong matches = 0;
for (int y = region.Y; y < Math.Min(bmp.Height, region.Y + region.Height); y++)
{
for (int x = region.X; x < Math.Min(bmp.Width, region.X+Region.Width); x++)
{
if (bm.GetPixel(x, y) == target_color)
{
matches++;
}
}
}
return matches;
}
Determine the format of an image file?
see my answer here:
Find image format using Bitmap object in C#
using System.Linq;
//...
//get image
var file_bytes = System.Convert.FromBase64String(@"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
var file_stream = new System.IO.MemoryStream(file_bytes);
var file_image = System.Drawing.Image.FromStream(file_stream);
//get image format
var file_image_format = typeof(System.Drawing.Imaging.ImageFormat).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).ToList().ConvertAll(property => property.GetValue(null, null)).Single(image_format => image_format.Equals(file_image.RawFormat));
System.Diagnostics.Debug.WriteLine(file_image_format, "file_image_format");
//get image codec
var file_image_format_codec = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders().ToList().Single(image_codec => image_codec.FormatID == file_image.RawFormat.Guid);
System.Diagnostics.Debug.WriteLine(file_image_format_codec.CodecName + ", mime: " + file_image_format_codec.MimeType + ", extension: " + file_image_format_codec.FilenameExtension, "image_codecs", "file_image_format_type");
Can bitmap object be save as PNG or JPEG file format
Bitmap extends Image, therefore you can call: Image.Save (String, ImageFormat)
. For example:
using System.Drawing
// ...
Bitmap img = new Bitmap("file.jpg");
img.Save("file.png", ImageFormat.Png); // ImageFormat.Jpeg, etc
Omitting the second argument and just calling Image.Save(String)
will save the image as its raw format.
How to convert Bitmap to Image
A Bitmap
is an Image
. It inherits from the Image
class.
From MSDN:
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class Bitmap : Image
Related Topics
Why Does System.Threading.Timer Stop on Its Own
When Not to Use Yield (Return)
How to Render an ASP.NET MVC View in PDF Format
Filter a List by Another List C#
Change Custom Attribute's Parameter at Runtime
How to Update an Existing Windows Phone 8 App to Windows Phone Store 8.1
Sending Sms from an ASP.NET Website
C# Flickering Listview on Update
How to Parse Dates with a Suffix "Th", "St" or "Nd" on the Day of the Month
Visual Studio Installer > How to Launch App at End of Installer
C#: Getting Names of Properties in a Chain from Lambda Expression
Proper Datagrid Search from Textbox in Wpf Using Mvvm
Use Local Images in Webbrowser Control
Best Way to Share Data Between Two Child Components in Blazor
JSON Deserialization - Map Array Indices to Properties with JSON.Net