zoukankan      html  css  js  c++  java
  • WPF Image显示本地照片 判断选择删除

    程序来自做拍照软件,用户拍照的时候,可以先看效果图,再选择是否要这张,我的思路是,先保存,之后显示,最后选择是否确定,不要的话,再删除.

    刚开始选择了Uri这个方法,没通,因为Uri连的是程序内资源,并非本地

    不过也把方法放着,来自:http://www.cnblogs.com/ErinCodeMM/archive/2011/04/07/2008819.html

         WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源。其中较为常见的情况是用Uri加载图像。Uri表达式的一般形式为:协议+授权+路径
         协议:pack://
         授权:有两种。一种用于访问编译时已经知道的文件,用application:///。一种用于访问编译时不知道、运行时才知道的文件,用siteoforigin:///。在这里加载图片时,我们选用前者,即application:///,但是书写时候,我们一般用逗号代替斜杠,也就是改写作application:,,,。
         路径:分为绝对路径和相对路径。这里我们选用相对路径,普适性更强。
    
         下面,我们举一个简单的例子:
              pack://application:,,,/images/my.jpg
         当然,WPF默认Uri设置有pack://application:,,,,所以我们也可以直接将其写作:
              /images/my.jpg
         后边写例子程序时,为了让读者更好的了解Uri,我们都采用完整的Uri写法。
    
         下面在讲讲装载图片的两种方式,一种用XAML引用资源,一种用代码引用资源。
    
         用XAML引用资源:
              <Image Source="pack://application:,,,/images/my.jpg"/>
    
         用代码引用资源:
              Image img;
              img.Source=new BitmapImage(new Uri("pack://application:,,,/images/my.jpg"),UriKind.Relative);
    
    
         我们写一个例子代码。在其中运用XAML,代码两种方式引用资源。
    
    Window1.xaml
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <Window x:Class="testURI.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="400" Width="240">
        <!--堆积面板是最简单的控制面板-->
        <StackPanel>
            <!--1.XAML中引用图片资源-->
            <!--也可用Image Name="image1" Source="pack://application:,,,/images/1.jpg" Height="165" Width="220"/-->
            <Image Name="image1" Source="pack://application:,,,/images/1.jpg" Height="165" Width="220"/>
             
            <!--定义Image对象,但是没有指定图片源,待在代码中指定Source源-->
            <Image Name="image2" Height="165" Width="220"/>
        </StackPanel>
    </Window>
     Window1.xaml.cs
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    namespace testURI
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                //2.代码中引用图片资源  
                image2.Source = new BitmapImage(new Uri("/images/2.jpg", UriKind.Relative));
            }
        }
    }
    WPF之Uri加载图片

    下面这段代码来自:http://www.cnblogs.com/yunyou/archive/2013/01/25/2876054.html

    private void SetSource(System.Windows.Controls.Image image, string fileName)
     2         {
     3             System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(fileName);
     4             int imageWidth = 0, imageHeight = 0;
     5             InitializeImageSize(sourceImage, image, out imageWidth, out imageHeight);
     6           
     7             Bitmap sourceBmp = new Bitmap(sourceImage, imageWidth, imageHeight);
     8             IntPtr hBitmap = sourceBmp.GetHbitmap();
     9             BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,
    10                    BitmapSizeOptions.FromEmptyOptions());
    11             bitmapSource.Freeze();
    12             WriteableBitmap writeableBmp = new WriteableBitmap(bitmapSource);
    13             sourceImage.Dispose();
    14             sourceBmp.Dispose();
    15             image.Source = writeableBmp;
    16         }
    17 
    18         /// <summary>
    19         /// Initialize ImageSize.
    20         /// </summary>
    21         /// <param name="sourceImage"></param>
    22         /// <param name="image"></param>
    23         /// <param name="imageWidth"></param>
    24         /// <param name="imageHeight"></param>
    25         private static void InitializeImageSize(System.Drawing.Image sourceImage, System.Windows.Controls.Image image,
    26             out int imageWidth, out int imageHeight)
    27         {
    28             int width = sourceImage.Width;
    29             int height = sourceImage.Height;
    30             float aspect = (float)width / (float)height;
    31             if (image.Height != double.NaN)
    32             {
    33                 imageHeight = Convert.ToInt32(image.Height);
    34                 imageWidth = Convert.ToInt32(aspect * imageHeight);
    35             }
    36             else if (image.Width != double.NaN)
    37             {
    38                 imageWidth = Convert.ToInt32(image.Width);
    39                 imageHeight = Convert.ToInt32(image.Width / aspect);
    40             }
    41             else
    42             {
    43                 imageHeight = 100;
    44                 imageWidth = Convert.ToInt32(aspect * imageHeight);
    45             }
    46         }
    WPF中Image显示本地图片

    这段是我自己软件实际的代码

                    System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(path);//path为地址
                    int imageWidth = 640, imageHeight = 480;//设置大小
    
                    Bitmap sourceBmp = new Bitmap(sourceImage, imageWidth, imageHeight);
                    IntPtr hBitmap = sourceBmp.GetHbitmap();
                    BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,
                           BitmapSizeOptions.FromEmptyOptions());
                    bitmapSource.Freeze();
                    WriteableBitmap writeableBmp = new WriteableBitmap(bitmapSource);
                    sourceImage.Dispose();
                    sourceBmp.Dispose();
                    this.Image.Source = writeableBmp;
                    delOrNot = true;

    接下来是判断是否想要,不要的话,删除

            public KinectWindow()
            {
                InitializeComponent();
                this.KeyUp += KinectWindow_KeyUp;//订阅删除键
            } 
    
            private void delPhoto_Click(object sender, RoutedEventArgs e)
            {
                if (delOrNot)
                {
                    System.IO.File.Delete(path);//删除代码
                    //恢复镜头拍摄
                    this.Image.Source = this.colorBitmap;
                    delOrNot = false;
                }
            }
    
            //拍照与删除照片按钮
            void KinectWindow_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.D)
                {
                    // 按下“删除”键
                    delPhoto_Click(this, null);
                }
                if (e.Key == Key.S)
                {
                    // 按下“拍摄”键
                    ButtonScreenshotClick(this, null);
                }
            }
  • 相关阅读:
    How To Configure Server Side Transparent Application Failover [ID 460982.1]
    10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
    AIX 系统介绍
    10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
    Parameter DIRECT: Conventional Path Export Versus Direct Path Export [ID 155477.1]
    Linux下 数据文件 效验问题
    open_links_per_instance 和 open_links 参数说明
    Oracle 外部表
    Export/Import DataPump Parameter ACCESS_METHOD How to Enforce a Method of Loading and Unloading Data ? [ID 552424.1]
    Linux下 数据文件 效验问题
  • 原文地址:https://www.cnblogs.com/bkycjj/p/3343538.html
Copyright © 2011-2022 走看看