如何加载目录的所有图片,使图片浏览器像Windows图片查看器? [英] How to load all pictures of a directory to make picture viewer like windows picture viewer?

查看:299
本文介绍了如何加载目录的所有图片,使图片浏览器像Windows图片查看器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!这是我在CodeProject上的第一个问题,我正在创建一个像windows图片查看器一样的程序来查看图像,如何让图片框加载目录图片,然后像点击Windows图片查看器一样点击按钮上的图像,(下一个按钮)显示下一个图像/上一个按钮显示上一个图像)??

解决方案

最简单的方法是决定是否只需要一个类型的图像文件(* .jpg,说 - 或* .bmp)或所有已知图像类型。

前者很简单: Directory.GetAllFiles [ ^ ]有一个重载,允许您指定搜索模式,因此它将返回仅JPG文件的路径列表:

  string  [] files = Directory.GetAllFiles( @ < span class = code-string> D:\ MyImages \,  *。jpg); 





第二个更难,因为你必须知道该系统上的图像文件是什么。

但是如果你看一下这个:所有支持图像的FileDialog.Filter生成器 [ ^ ]它显示了如何查找系统上所有支持的图像文件编解码器 - 您必须移动它以生成字符串扩展名列表,但这是非常明显的代码。

然后,使用Directory.GetAllFiles返回路径中的所有文件:

  string  [] files = Directory.GetAllFiles( @  D:\ MyImages \\ \\); 



然后循环找到所有具有正确扩展名的 - 这很简单:

  if (imageFileExtensions.C ontains(Path.GetExtension(fileInPath))
{
// 这是一个图像文件
...





一旦你有一个数组或图像文件列表,你就可以使用一个索引来选择下一个显示的内容,从列表中获取文件名,然后从文件中加载图像。显示是微不足道的。

每次按下一步按钮,增加索引(记住检查是否还有其他显示)并使用新值选择新文件。



有意义吗?


Hi guys! This is my first question on CodeProject, I am creating a program like windows picture viewer to view images, How can I make the picturebox load pictures of a directory and then change the image on button click just like the Windows Picture Viewer, (Next Button shows next image/Previous Button shows previous image)??

解决方案

Easiest way is to decide if you want just one "type" of image files (*.jpg, say - or *.bmp) or all "known" image types.
The former is easy: Directory.GetAllFiles[^] has an overload which allows you to specify a search pattern, so it will return a list of paths of only JPG files:

string[] files = Directory.GetAllFiles(@"D:\MyImages\", "*.jpg");



The second is harder, as you have to know what if considered an image file on that system.
However if you have a look at this: A FileDialog.Filter generator for all supported images[^] it shows how to find all the supported image file codecs on your system - you'd have to movifyy it to produce a list of string extension, but that's pretty obvious code.
Then, use Directory.GetAllFiles to return all the files in your path:

string[] files = Directory.GetAllFiles(@"D:\MyImages\");


And then loop though to find all the ones that have the correct extension - that's pretty simple:

if (imageFileExtensions.Contains(Path.GetExtension(fileInPath))
   {
   // It's an image file
   ...



Once you have an array or list of image files, you cae just use an index to select which to display next, get the file name from your list, and load the Image from the file. Display is then trivial.
And each time you press the "Next" button, to increment the index (remembering to check if there are any more to display) and use the new value to select a new file.

Make sense?


这篇关于如何加载目录的所有图片,使图片浏览器像Windows图片查看器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆