1. 程式人生 > >WPF中使用BitmapImage處理圖片檔案

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;