public MainWindow()
    {
      InitializeComponent();

      this.Loaded += main_Loaded;
      this.Drop += main_Drop;
      this.SizeChanged += main_SizeChanged;
      this.Closing += main_Unload;
      
      this.sldOpacity.ValueChanged += sldOpacity_ValueChanged;
      this.btnTitle.Click += lblTitle_DoubleClick;

      this.sizeX.Visibility = System.Windows.Visibility.Collapsed;
      this.sizeY.Visibility = System.Windows.Visibility.Collapsed;

      var setting = new ImageViewer.Properties.Settings();
      this.Left = setting.Left;
      this.Top = setting.Top;
      this.chkTopMost.IsChecked = setting.Topmost;
      this.sldOpacity.Value = setting.Opacity;
    }
    /*
    public static Bitmap ToBitmap(BitmapImage bitmapImage)
    {
      if (bitmapImage == null) return null;

      using (MemoryStream outStream = new MemoryStream())
      {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapImage));
        enc.Save(outStream);
        Bitmap bitmap = new Bitmap(outStream);

        return new Bitmap(bitmap);
      }
    }
    public static BitmapImage ToBitmapImage(Bitmap bitmap)
    {
      if (bitmap == null) return null;

      using (var memory = new MemoryStream())
      {
        bitmap.Save(memory, ImageFormat.Png);
        memory.Position = 0;

        var bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = memory;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();

        return bitmapImage;
      }
    }

    public static void ChangeToNegativeImage(Bitmap img)
    {
      //1ピクセルあたりのバイト数を取得する
      PixelFormat pixelFormat = img.PixelFormat;
      int pixelSize = Image.GetPixelFormatSize(pixelFormat) / 8;
      if (pixelSize < 3 || 4 < pixelSize)
      {
        throw new ArgumentException(
            "1ピクセルあたり24または32ビットの形式のイメージのみ有効です。",
            "img");
      }

      //Bitmapをロックする
      BitmapData bmpDate = img.LockBits(
          new Rectangle(0, 0, img.Width, img.Height),
          ImageLockMode.ReadWrite,
          pixelFormat);

      if (bmpDate.Stride < 0)
      {
        img.UnlockBits(bmpDate);
        throw new ArgumentException(
            "ボトムアップ形式のイメージには対応していません。",
            "img");
      }

      //ピクセルデータをバイト型配列で取得する
      IntPtr ptr = bmpDate.Scan0;
      byte[] pixels = new byte[bmpDate.Stride * img.Height];
      System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, pixels.Length);

      //すべてのピクセルの色を反転させる
      for (int y = 0; y < bmpDate.Height; y++)
      {
        for (int x = 0; x < bmpDate.Width; x++)
        {
          //ピクセルデータでのピクセル(x,y)の開始位置を計算する
          int pos = y * bmpDate.Stride + x * pixelSize;
          //青、緑、赤の色を変更する
          pixels[pos] = (byte)(255 - pixels[pos]);
          pixels[pos + 1] = (byte)(255 - pixels[pos + 1]);
          pixels[pos + 2] = (byte)(255 - pixels[pos + 2]);
        }
      }

      //ピクセルデータを元に戻す
      System.Runtime.InteropServices.Marshal.Copy(pixels, 0, ptr, pixels.Length);

      //ロックを解除する
      img.UnlockBits(bmpDate);
    }*/

    private void main_Unload(object sender, System.ComponentModel.CancelEventArgs e)
    {
      var setting = new ImageViewer.Properties.Settings();
      setting.Left = (int)this.Left;
      setting.Top = (int)this.Top;
      setting.Topmost = this.chkTopMost.IsChecked.Value;
      setting.Opacity = (ushort)this.sldOpacity.Value;
      setting.Save();
    }