chocolade
New member
- Joined
- Dec 15, 2020
- Messages
- 2
- Programming Experience
- 3-5
The code for checking :
but I'm not sure how to use it :
I want to make that if the image file is not gif type then convert the file to gif type.
I have already the conversion method and it's converting fine to gif but I want to check first if the file is not gif and only if it's not gif then convert it to gif but I'm not sure how to use the GetImageFormat.
C#:
public enum ImageFormat
{
bmp,
jpeg,
gif,
tiff,
png,
unknown
}
public static ImageFormat GetImageFormat(byte[] bytes)
{
var bmp = Encoding.ASCII.GetBytes("BM"); // BMP
var gif = Encoding.ASCII.GetBytes("GIF"); // GIF
var png = new byte[] { 137, 80, 78, 71 }; // PNG
var tiff = new byte[] { 73, 73, 42 }; // TIFF
var tiff2 = new byte[] { 77, 77, 42 }; // TIFF
var jpeg = new byte[] { 255, 216, 255, 224 }; // jpeg
var jpeg2 = new byte[] { 255, 216, 255, 225 }; // jpeg canon
if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
return ImageFormat.bmp;
if (gif.SequenceEqual(bytes.Take(gif.Length)))
return ImageFormat.gif;
if (png.SequenceEqual(bytes.Take(png.Length)))
return ImageFormat.png;
if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
return ImageFormat.tiff;
if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
return ImageFormat.tiff;
if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
return ImageFormat.jpeg;
if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
return ImageFormat.jpeg;
return ImageFormat.unknown;
}
but I'm not sure how to use it :
C#:
images = Directory.GetFiles(@"d:\satimages", "*.jpg");
int counter = 0;
foreach (string file in images)
{
using (var img = Image.FromFile(file))
{
GetImageFormat(
}
}
I want to make that if the image file is not gif type then convert the file to gif type.
I have already the conversion method and it's converting fine to gif but I want to check first if the file is not gif and only if it's not gif then convert it to gif but I'm not sure how to use the GetImageFormat.