我正在从文件中加载图像,我想知道在从文件中完全读取图像之前如何验证图像.
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) 我有这个文件类型过滤器:
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) 我想验证目录中的所有文件是否属于某种类型.到目前为止我做了什么.
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) 我有一个图像调整大小的程序,它的工作原理.问题是当用户在文件选择对话框中选择非图像文件时,它会崩溃.如何查看图像文件?
在C#.NET 4.0,基于我下载虚假图像的问题(将错误的aspx页面保存为image.jpg而不是image.jpg中的实际图像),我需要以某种方式读取文件并确定它是否是有效图像.我只需要1个函数public bool IsValidJpgImage(string ImageFilename);返回false的任何东西(不是有效的图像文件)我将从磁盘中删除.
image ×4
c# ×3
validation ×2
.net ×1
file ×1
file-exists ×1
file-io ×1
file-type ×1
filefilter ×1
java ×1
jpeg ×1
vb.net ×1