请调整图片代码的大小 [英] please resizing image code

查看:87
本文介绍了请调整图片代码的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作项目要将许多小图像保存为一个大图像,我使用了以下代码:

I''m making project To save many small images into one large image and I used this code :

private void Openbutton_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            string selected = folderBrowserDialog1.SelectedPath;
            string[] imagefilelist = Directory.GetFiles(selected);

            int wid = 0, hei = 0, med = 0;
            foreach (string imagefile in imagefilelist)
            {
                if (Image.FromFile(imagefile) != null)
                {
                    Image.FromFile(imagefile).Dispose();
                }
                PictureBox eachpicturebox = new PictureBox();
                imgs = eachpicturebox;
                eachpicturebox.Size = new Size(241, 155);
                eachpicturebox.Location = new Point(wid * 241 + med, hei);
                eachpicturebox.Image = Image.FromFile(imagefile);
                wid++;
                if (wid == 3)
                {
                    wid = 0;
                    hei += 155;
                }
                panel1.Controls.Add(eachpicturebox);
            }
        }



该代码为每个图像以及放置在面板中的所有图片框创建图片框.
这里的问题是:我希望能够更改小图像的大小.

谁能帮助我.



This code creates picture box for each image and all picture boxes placed in the panel1.
The problem here is: I want to be able to change the size of small images.

Can anyone help me.

推荐答案

首先,不要使用单个图片框-效率不高.
其次,为什么要这样做:
First, don''t use individual pictureboxes - it is not very efficient.
Secondly, why are you doing this:
if (Image.FromFile(imagefile) != null)
{
    Image.FromFile(imagefile).Dispose();
}

if条件读取文件,将其转换为新的Image,进行比较,然后丢弃引用.如果它不为null(始终为true),它将再次读取它,创建另一个新Image,然后立即将其处置.
您认为这合理吗?

代替使用PictureBoxes,处理Panel的Paint事件,然后将图像绘制到提供的Graphics对象上.这将使您可以更好地控制图像的位置和大小,并且可以更快地进行控制.

The if condition reads a file, converts it to a new Image, compares it and then throws away the reference. If it isn''t null - which will always be true - it reads it again, creates another new Image and immediately Disposes it.
What do you think that does that is reasonable?

Instead of using PictureBoxes, handle the Paint event for the Panel, and draw the images onto the supplied Graphics object. This will allow you much better control over the placement and size of the images, as well as being considerably quicker.


创建一个加载图像并显示图像的用户控件.然后,您只需为控件提供一个包含文件的目录名,然后让它在后台运行即可为您完成工作,从而使您的表单代码更加整洁.

Create a user control that loads the images and displays them. Then you can just give the control a directory name that contains the files, and let it do the work for you behind the scense, thus uncluttering your form code.

public class MyControl : Control
{
    private List<image> images = new List<image>();

    public MyControl(){}

    public MyControl(string folder)
    {
        FileInfo[] files = Diretory.GetFiles("*.jpg");
        foreach (FileInfo info in files)
        {
            Image img = Image.FromFile(info.FullName);
            if (img != null)
            {
                images.Add(img);
            }
        }
    }
}</image></image>



此时,您可以对图像进行任何操作.



At this point, you can do anything you want with the images.


这篇关于请调整图片代码的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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