I wrote a blog to get the correct format of the picture. The code shown in the blog has been working well. Until today, when deploying the program to alicloud function computing, the following error occurred:
System.Drawing is not supported on this platform.
This shows that we can’t use GDI + related functions on the alicloud function computing server. Even so, we can still get the image format by reading the file header
public static class ImageHelper
{
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;
}
}
new ImageHelper
You need a binary array as a parameter, but that doesn’t mean you need to read all the contents of the file into memory. Using the following code can get better running effect:
var fn = @"D:\1.jpg";
using (var fs = File.OpenRead(fn))
{
var header = new byte[10];
await fs.ReadAsync(header, 0, 10);
var ext = ImageHelper.GetImageFormat(header);
ext.Dump();
}
Read More:
- use cin.get () instead of system (“pause”) to avoid the flash of C + + programs
- C + + pauses the black window system (“pause”); (get ch(), getchar(), system (pause)’s connection and difference
- How to use C + + function pointer array
- C#: How to Use Httpclient to upload files with other parameters
- How to Use Apt get Command Under Mac OSX
- How to Use ffmpeg to convert MP4 and other formats to MP3 format
- FTP use get command 550 failed to open file, but use the put command to solve the problem
- “Typeerror: invalid dimensions for image data” in Matplotlib drawing imshow() function
- [windows] U disk installed system, unable to format the selected partition [error: 0x8004242d]
- How to use it RegAsm.exe Register C in a development / production environment#/ VB.NET Developed DLL
- How to Solve apt get update problem: usr/lib/apt/methods/HTTPS could not be found_PUBKEY 7EA0A9C3F273FCD8
- How to convert iconfont to PNG transparent image
- “Could not import PIL.Image . The use of array_ to_ The solution of img requires PIL. “
- Convert Tencent video QLV format to MP4 format
- How to Solve Yolov5 Common Error (Three Errors)
- To solve the problem that deep in 20.2 system cannot save and transform the format of typora, and provide an effective installation method of typora
- How to Use Truffle to Deploy contracts on moonbeam
- Extract audio from ffmpeg video to mp3 format or m4a format command
- Viewing the file system format of disk partition under Linux
- Solution to msxml3.dll error ‘800c0008’ the system cannot locate the resource specified