基本信息
源码名称:WPF 三维图片墙效果
源码大小:2.63M
文件格式:.rar
开发语言:C#
更新时间:2020-05-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

使用WPF实现3D图片墙效果

using System;
using System.Windows;
using System.Windows.Media.Media3D;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace _3DPictureBrowser
{
    class Picture3D : ModelVisual3D
    {
        public int index;
        public int sample_W = 5;
        public int sample_H = 5;

        public double startAlpha;
        public double startY;
        public double raduis;
        public double top;
        public double width;
        public double height;

        public MeshGeometry3D mesh;
        public ImageBrush brush;
        public DiffuseMaterial material;
        public GeometryModel3D geomod;

        public RotateTransform3D desRotateTransform3D;
        public TranslateTransform3D desTranslateTransform3D;

        int plainWidth = 10;
        int plainHeight = 8;

        public static BitmapSource CreateResizedImage(ImageSource source, int width, int height)
        {
            System.Windows.Rect rect = new System.Windows.Rect(0, 0, width, height);

            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())
            {
                drawingContext.DrawImage(source, rect);
            }

            RenderTargetBitmap resizedImage = new RenderTargetBitmap(
                (int)rect.Width, (int)rect.Height,
                96, 96,
                PixelFormats.Default);
            resizedImage.Render(drawingVisual);

            return resizedImage;
        }

        public Picture3D(int ind, double t, double sa, double sy, double r, double preWidth, double preHeight, String textureUrl)
        {
            index = ind;
            top = t;
            startAlpha = sa;
            startY = sy;
            raduis = r;

            brush = new ImageBrush();
            BitmapImage bitmapImage = new BitmapImage(new Uri(textureUrl, UriKind.Relative));
            if (bitmapImage.Width > 5120)
            {
                int newWidth = 512;
                int newheight = (int)(newWidth * bitmapImage.Height / bitmapImage.Width);
                brush.ImageSource = CreateResizedImage(bitmapImage, newWidth, newheight);
            }
            else
                brush.ImageSource = bitmapImage;

            material = new DiffuseMaterial();
            material.Brush = brush;

            double realWidth = brush.ImageSource.Width;
            double realHeight = brush.ImageSource.Height;

            if (realWidth > realHeight)
            {
                width = preWidth;
                height = width * realHeight / realWidth;

                startY -= (preHeight - height) / 2;
            }
            else
            {
                height = preHeight;
                width = height * realWidth / realHeight;

                startAlpha -= (preWidth - width) / r / 2;
            }

            mesh = new MeshGeometry3D();

            double da = (width / r) / sample_W;
            double dh = height / sample_H;


            for (int i = 0; i < sample_W; i )
                for (int j = 0; j < sample_H; j )
                {
                    Point3D p = new Point3D(r * Math.Cos(-da * i startAlpha), -j * dh startY, r * Math.Sin(-da * i startAlpha));
                    mesh.Positions.Add(p);
                    mesh.TextureCoordinates.Add(new Point(i / (sample_W - 1.0), j / (sample_H - 1.0)));
                }


            for (int i = 0; i < sample_W - 1; i )
            {
                for (int j = 0; j < sample_H - 1; j )
                {
                    mesh.TriangleIndices.Add((i 1) * sample_H j 1);
                    mesh.TriangleIndices.Add((i 1) * sample_H j);
                    mesh.TriangleIndices.Add(i * sample_H j);

                    mesh.TriangleIndices.Add(i * sample_H j);
                    mesh.TriangleIndices.Add(i * sample_H j 1);
                    mesh.TriangleIndices.Add((i 1) * sample_H j 1);

                }
            }

            // Define the GeometryModel3D.
            geomod = new GeometryModel3D();
            geomod.Geometry = mesh;
            geomod.Material = material;
            geomod.BackMaterial = material;// new DiffuseMaterial(Brushes.Green);

            // Create ModelVisual3D for GeometryModel3D.
            this.Content = geomod;
            Transform3DGroup transform3DGroup = new Transform3DGroup();
            transform3DGroup.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0)));
            transform3DGroup.Children.Add(new TranslateTransform3D(0, 0, 0));
            this.Transform = transform3DGroup;

            desRotateTransform3D = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), (startAlpha - Math.PI / 2) * 180 / Math.PI));
            desTranslateTransform3D = new TranslateTransform3D();

            int index1 = ind / (plainHeight * plainWidth);
            int index2 = (ind % (plainHeight * plainWidth)) / plainWidth;
            int index3 = (ind % (plainHeight * plainWidth)) % plainWidth;

            desTranslateTransform3D.OffsetZ = -index1 * preWidth * 5 - r;
            desTranslateTransform3D.OffsetX = (index3 - plainWidth / 2) * preWidth (preWidth - width) / 2;
            desTranslateTransform3D.OffsetY = top - index2 * preWidth - (preHeight - height) / 2 - startY;

        }
    }
}