相关疑难解决方法(0)

在C#中验证文件中的图像

我正在从文件中加载图像,我想知道在从文件中完全读取图像之前如何验证图像.

string filePath = "image.jpg";
Image newImage = Image.FromFile(filePath);
Run Code Online (Sandbox Code Playgroud)

当image.jpg不是真正的jpg时会出现问题.例如,如果我创建一个空文本文件并将其重命名为image.jpg,则在加载image.jpg时将抛出OutOfMemory Exception.

我正在寻找一个功能,它将在给定图像的流或文件路径的情况下验证图像.

示例函数原型

bool IsValidImage(string fileName);
bool IsValidImage(Stream imageStream);
Run Code Online (Sandbox Code Playgroud)

.net c# file-io image

51
推荐指数
7
解决办法
8万
查看次数

检查文件扩展名是否为图像的好方法

我有这个文件类型过滤器:

    public const string Png = "PNG Portable Network Graphics (*.png)|" + "*.png";
    public const string Jpg = "JPEG File Interchange Format (*.jpg *.jpeg *jfif)|" + "*.jpg;*.jpeg;*.jfif";
    public const string Bmp = "BMP Windows Bitmap (*.bmp)|" + "*.bmp";
    public const string Tif = "TIF Tagged Imaged File Format (*.tif *.tiff)|" + "*.tif;*.tiff";
    public const string Gif = "GIF Graphics Interchange Format (*.gif)|" + "*.gif";
    public const string AllImages = "Image file|" + "*.png; *.jpg; *.jpeg; *.jfif; *.bmp;*.tif; *.tiff; *.gif";
    public …
Run Code Online (Sandbox Code Playgroud)

c# file-extension openfiledialog filefilter

20
推荐指数
4
解决办法
5万
查看次数

检查文件是否属于某种类型

我想验证目录中的所有文件是否属于某种类型.到目前为止我做了什么.

private static final String[] IMAGE_EXTS = { "jpg", "jpeg" };

private void validateFolderPath(String folderPath, final String[] ext) {

        File dir = new File(folderPath);

        int totalFiles = dir.listFiles().length;

        // Filter the files with JPEG or JPG extensions.
        File[] matchingFiles = dir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(ext[0])
                        || pathname.getName().endsWith(ext[1]);
            }
        });

        // Check if all the files have JPEG or JPG extensions
        // Terminate if validation fails.
        if (matchingFiles.length != totalFiles) {
            System.out.println("All the tiles should be …
Run Code Online (Sandbox Code Playgroud)

java file-extension jpeg image file-exists

5
推荐指数
1
解决办法
2882
查看次数

在VB.NET中检查文件类型?

我有一个图像调整大小的程序,它的工作原理.问题是当用户在文件选择对话框中选择非图像文件时,它会崩溃.如何查看图像文件?

vb.net validation file-type image

3
推荐指数
2
解决办法
2万
查看次数

如何在尝试加载之前检查jpeg图像文件是否实际上是有效图像?

C#.NET 4.0,基于我下载虚假图像的问题(将错误的aspx页面保存为image.jpg而不是image.jpg中的实际图像),我需要以某种方式读取文件并确定它是否是有效图像.我只需要1个函数public bool IsValidJpgImage(string ImageFilename);返回false的任何东西(不是有效的图像文件)我将从磁盘中删除.

c# validation image file

3
推荐指数
1
解决办法
1万
查看次数