复制Windows 7图像文件夹3d缩略图

时间:2011-10-31 22:28:52

标签: windows-7 3d thumbnails

我想以编程方式从图像文件夹创建缩略图,这看起来类似于Windows 7图像文件夹样式,就像我上传的图片一样。我有一个例程,它将在彼此之上添加图像,并在x轴上旋转它们,但我认为我需要一些Y旋转或某些东西来使这个幻觉完整。我实际上已经使用带有缩略图方法的Windows7APICodePack完成了这项操作,但它似乎迫使您在大图标模式下使用Explorer。我不希望这依赖于Explorer。我也不想使用WPF(ViewPort3d)。这是我想要的样子: FinalImage http://www.chuckcondron.com/folderexample.JPG

这是我到目前为止所做的: StitchedImageThumb http://www.chuckcondron.com/stitchedImageThumb.jpg

正如你所看到的那样,我的尝试不是那么漂亮,照片也不会出现在奶油色文件夹中(真的可以使用实际的文件夹图片,但不知道如何做到这一点)。

当前代码(c#)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;


namespace ThumbnailCreator
{
public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //get all the files in a directory
        string[] files = Directory.GetFiles(@"C:\MyImages");

        //combine them into one image
        Bitmap stitchedImage = Combine(files);

        if(File.Exists("stitchedImage.jpg")) File.Delete("stitchedImage.jpg");
        //save the new image
        stitchedImage.Save(@"stitchedImage.jpg", ImageFormat.Jpeg);

        if (File.Exists("stitchedImage.jpg"))
        {
            FileStream s = File.Open("stitchedImage.jpg", FileMode.Open);
            Image temp = Image.FromStream(s);
            s.Close();
            pictureBox1.Image = temp;
        }
        CreateThumb(@"stitchedImage.jpg", @"stitchedImageThumb.jpg");
    }

    public Bitmap Combine(string[] files)
    {
        //read all images into memory
        List<Bitmap> images = new List<Bitmap>();
        Bitmap finalImage = null;

        try
        {
            int width = 0;
            int height = 0;
            int rotate = 7;

            foreach (string image in files)
            {
                //create a Bitmap from the file and add it to the list
                Bitmap bitmap = new Bitmap(image);
                bitmap = RotateImage(bitmap, rotate);
                //update the size of the final bitmap
                //width += bitmap.Width;
                width = 2500;
                //height = bitmap.Height > height ? bitmap.Height : height;
                height = 1000;
                images.Add(bitmap);
                rotate += 5;
            }
            //create a bitmap to hold the combined image
            finalImage = new Bitmap(width, height);

            //get a graphics object from the image so we can draw on it
            using (Graphics g = Graphics.FromImage(finalImage))
            {
                //set background color
                g.Clear(Color.Goldenrod);
                //go through each image and draw it on the final image
                ImageAttributes ia = new ImageAttributes();
                ColorMatrix cm = new ColorMatrix();
                cm.Matrix33 = 0.75f;
                ia.SetColorMatrix(cm);

                foreach (Bitmap image in images)
                {
                    Image rotatedImage = new Bitmap(image);
                    g.DrawImage(rotatedImage, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ia);
                }
            }
            return finalImage;
        }
        catch (Exception ex)
        {
            if (finalImage != null)
                finalImage.Dispose();

            throw ex;
        }
        finally
        {
            //clean up memory
            foreach (System.Drawing.Bitmap image in images)
            {
                image.Dispose();
            }
        }
    }

    public void CreateThumb(string source, string destination)
    {
        Image imgThumb = null;
        try
        {
            Image image = null;
            // Check if image exists
            image = Image.FromFile(source);
            if (image != null)
            {
                imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());
                imgThumb.Save(destination);
                image.Dispose();
            }
        }
        catch
        {
            MessageBox.Show("An error occured");
        }

        if (File.Exists(destination))
        {
            FileStream s = File.Open(destination, FileMode.Open);
            Image temp = Image.FromStream(s);
            s.Close();
            pictureBox2.Image = temp;
        }
    }

    private Bitmap RotateImage(Bitmap inputImg, double degreeAngle)
    {
        //Corners of the image
        PointF[] rotationPoints = { new PointF(0, 0),
                                    new PointF(inputImg.Width, 0),
                                    new PointF(0, inputImg.Height),
                                    new PointF(inputImg.Width, inputImg.Height)};

        //Rotate the corners
        PointMath.RotatePoints(rotationPoints, new PointF(inputImg.Width / 2.0f, inputImg.Height / 2.0f), degreeAngle);

        //Get the new bounds given from the rotation of the corners
        //(avoid clipping of the image)
        Rectangle bounds = PointMath.GetBounds(rotationPoints);

        //An empy bitmap to draw the rotated image
        Bitmap rotatedBitmap = new Bitmap(bounds.Width *2, bounds.Height *2);

        using (Graphics g = Graphics.FromImage(rotatedBitmap))
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            //Transformation matrix
            Matrix m = new Matrix();
            m.RotateAt((float)degreeAngle, new PointF(inputImg.Width / 2.0f, inputImg.Height / 2.0f));
            m.Translate(-bounds.Left, -bounds.Top, MatrixOrder.Append); //shift to compensate for the rotation

            g.Transform = m;
            g.DrawImage(inputImg, 0, 0);
        }
        return rotatedBitmap;
    }
}
}

0 个答案:

没有答案