C# WPF 画像ビューア myScrView.csproj2022年08月19日 17:57

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
    <ApplicationIcon>ico\myPictureView_ICO.ico</ApplicationIcon>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="ico\myPictureView_ICO.ico" />
  </ItemGroup>

</Project>



C# WPF 画像ビューア App.xaml.cs2022年08月19日 17:59

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace myScrView
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        [STAThread]
        public static void Main(string[] args)
        {
            var app = new App();
            app.InitializeComponent();
            app.Run();
        }
    }
}



C# WPF 画像ビューア mainWindow.xaml2022年08月19日 18:01



<Window x:Name="mainWindow" x:Class="myScrView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:myScrView"
        mc:Ignorable="d"
        Title="myScrView" Height="600" Width="800">
    <Grid>
        <Grid x:Name="grd1" AllowDrop="True" Drop="grd1_Drop">
            <Grid.RowDefinitions>
                <RowDefinition Height="26"/>
                <RowDefinition/>
                <RowDefinition Height="27.76"/>
            </Grid.RowDefinitions>
            <Button x:Name="btnOpen" Content=" OpenFile " HorizontalAlignment="Left" Height="26" Margin="10,0,0,0" VerticalAlignment="Center" Width="68" Click="btnOpen_Click"/>
            <ScrollViewer x:Name="scrollViewer" Margin="0,0,0,0" Grid.Row="1" Background="#FFF7EF97"
                          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                <Canvas x:Name="canvas" Margin="0,0,0,0" Background="#FF9EFDDC"
                        PreviewMouseWheel="canvas_PreviewMouseWheel" >
                    <Image x:Name="image" SizeChanged="image_SizeChanged" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="image_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="image_PreviewMouseLeftButtonUp" PreviewMouseMove="image_PreviewMouseMove" />
                </Canvas>
            </ScrollViewer>
            <Label x:Name="lbl1" Content="File Drop Open , zoom(ctrl+wheel) , prev-next(wheel)" HorizontalAlignment="Left" Height="26" Margin="371,0,0,0" VerticalAlignment="Center" Width="355"/>
            <Label x:Name="lblScale" Content="scale" HorizontalAlignment="Left" Height="28" Margin="10,0,0,0" Grid.Row="2" VerticalAlignment="Center" Width="742"/>
            <Button x:Name="btnMae" Content="prev" HorizontalAlignment="Left" Height="26" Margin="97,0,0,0" VerticalAlignment="Center" Width="59" Click="btnMae_Click"/>
            <Button x:Name="btnTugi" Content="next" HorizontalAlignment="Left" Height="26" Margin="161,0,0,0" VerticalAlignment="Center" Width="60" Click="btnTugi_Click"/>
        </Grid>
    </Grid>
</Window>



C# WPF 画像ビューア mainWindow.xaml.cs2022年08月19日 18:02

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using Microsoft.Win32;  // オープンファイル・ダイアログボックスで使用する。
using System.IO;        // ファイル操作で使用する。
using System.Collections;

namespace myScrView
{
    public partial class MainWindow : Window
    {
        Point MyPoint;//マウスクリックの位置用
        double scale = 1.0;
        string openFile;
        string folda;
        List<string> failData = new List<string>();
        int filkaz, ban;
        List<string> extList = new List<string>() {
                ".jpg",".jpeg",".png",".bmp",".gif",".webp",".ico",".tiff" };

        public MainWindow()
        {
            string[] args = Environment.GetCommandLineArgs();
            InitializeComponent();
            filkaz = 0;
            if (args.Length > 1) fileOpen(args[1]);
        }

        private void fileOpen(string filmei)
        {
            var source = new BitmapImage();
            source.BeginInit();
            source.UriSource = new Uri(filmei);
            source.EndInit();
            image.Source = source;
            mainWindow.Title = filmei;
            folda = System.IO.Path.GetDirectoryName(filmei);
            //チェックlblScale.Content = "directory=" + folda;
            // 例:C:\testの全ファイル名を取得
            IEnumerable files = Directory.EnumerateFiles(folda, "*", SearchOption.AllDirectories);
            // ファイル名を取得
            foreach (string file in files)
            {
                if (extCheck(file) == true)
                {
                    ++filkaz;
                    failData.Add(file);
                }
            }
            ban = 0;
            lblScale.Content = "file=" + (ban+1) + "/" + filkaz;
            //チェックlblScale.Content = "dir=" + folda+ ",kaz=" + filkaz + "filmei0=" + failData[0];
        }

        private bool extCheck(string filmei)
        {
            string sdmy;
            foreach (string kakucho in extList)
            {
                sdmy = System.IO.Path.GetExtension(filmei).ToLower(); // 例:文字列ext1は「.jpg」
                if (kakucho == sdmy) return true;
            }
            return false;
        }

        private void niniFileOpen(string filmei)
        {
            var source = new BitmapImage();
            source.BeginInit();
            source.UriSource = new Uri(filmei);
            source.EndInit();
            image.Source = source;
            mainWindow.Title = filmei;
        }

        private void image_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            canvas.Height = e.NewSize.Height;
            canvas.Width = e.NewSize.Width;
        }

        private void canvas_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {            
            e.Handled = true; // 後続のイベントを実行しないための処理

            if ((Keyboard.Modifiers & ModifierKeys.Control) > 0)
            {           
                if (0 < e.Delta) {scale = 1.2; /* 20%拡大 */}
                else {scale = 0.8; /* 20%縮小 */}
                // 画像の拡大縮小
                image.Height = image.ActualHeight * scale;
                image.Width = image.ActualWidth * scale;                
                // マウス位置が中心になるようにスクロールバーの位置を調整
                Point mousePoint = e.GetPosition(scrollViewer);
                double x_barOffset = (scrollViewer.HorizontalOffset + mousePoint.X) * scale - mousePoint.X;
                scrollViewer.ScrollToHorizontalOffset(x_barOffset);
                double y_barOffset = (scrollViewer.VerticalOffset + mousePoint.Y) * scale - mousePoint.Y;
                scrollViewer.ScrollToVerticalOffset(y_barOffset);
            }
            else
            {
                if (0 < e.Delta) {maeGazo(); /* 前画像 */}
                else {tugiGazo(); /* 次画像 */}
            }
            lblScale.Content = "file=" + (ban+1) + "/" + filkaz + ",canvas.Width=" + (int)canvas.Width;
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "画像|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.webp;*.ico;*.tiff" ;
            if (dlg.ShowDialog() == false) return;
            fileOpen(dlg.FileName);
        } 

        private void grd1_Drop(object sender, DragEventArgs e)
        {
            string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
            if (files is null) return;
            fileOpen(files[0]);
        }

        private void image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {            
            MyPoint = e.GetPosition(this); //クリック位置取得
            //マウスがScrollViewer外になってもドラッグ移動を有効にしたいときだけ必要
            image.CaptureMouse();
        }

        private void image_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            image.Cursor = Cursors.Arrow; //カーソル形状を矢印に戻す
            //マウスがScrollViewer外になってもドラッグ移動を有効にしたいときだけ必要
            image.ReleaseMouseCapture();
        }

        private void image_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            //マウスドラッグ移動の距離だけスクロールさせるには
            //(直前のカーソル位置 - 今のカーソル位置) + (スクロールバーのOffset位置)
            //この値をSetOffsetする
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                image.Cursor = Cursors.ScrollAll; //カーソル形状を変更                
                var mouseP = e.GetPosition(this); //今のマウスの座標
                //マウスの移動距離=直前の座標と今の座標の差
                var xd = MyPoint.X - mouseP.X;
                var yd = MyPoint.Y - mouseP.Y;
                //移動距離+今のスクロール位置
                xd += scrollViewer.HorizontalOffset;
                yd += scrollViewer.VerticalOffset;
                //スクロール位置の指定
                scrollViewer.ScrollToHorizontalOffset(xd);
                scrollViewer.ScrollToVerticalOffset(yd);
                MyPoint = mouseP;//直前の座標を今の座標に変更
            }
        }

        private void tugiGazo()
        {
            if (++ban > filkaz - 1) ban = 0;
            niniFileOpen(failData[ban]);
        }

        private void maeGazo()
        {
            if (--ban < 0) ban = filkaz - 1;
            niniFileOpen(failData[ban]);
        }

        private void btnMae_Click(object sender, RoutedEventArgs e)
        {
            maeGazo();
        }

        private void btnTugi_Click(object sender, RoutedEventArgs e)
        {
            tugiGazo();
        }
    }
}



<< 2022/08
01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

このブログについて

ネットで見つけたいろいろ雑記

バックナンバー

RSS