WPF中使用BitmapImage处理图片文件

(1)图片从文件导入

                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.UriSource = new Uri(filename);
                image.EndInit();

 

(2)图片从Binary导入

                //已知Binary binary

                MemoryStream ms = new MemoryStream(binary.ToArray());
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = ms;
                image.EndInit();

 

(3)保存图片文件

                //已知BitmapImage image

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));

                FileStream fileStream = new FileStream(imageFileName, FileMode.Create, FileAccess.ReadWrite);
                encoder.Save(fileStream);
                fileStream.Close();

 

(4)图片应用到材质

                //已知BitmapImage image

                ImageBrush imageBrush = new ImageBrush(image);
                DiffuseMaterial material = new DiffuseMaterial();
                material.Brush = imageBrush;

你可能感兴趣的:(image,WPF)