首页 > 编程语言 >WPF C# split picture into small pieces and show in grid cells

WPF C# split picture into small pieces and show in grid cells

时间:2024-08-27 11:53:25浏览次数:5  
标签:picture show C# System Windows using new col row

using System;
using System.Collections.Generic;
using System.Drawing;
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 Rectangle = System.Drawing.Rectangle;
using System.IO;

namespace WpfApp288
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        List<List<string>> imgsList = new List<List<string>>();
        public MainWindow()
        {
            InitializeComponent();
            WindowState = WindowState.Maximized;
            SplitBigPicture();
            ShowSplitImageInGridCells();
        }

        private void ShowSplitImageInGridCells()
        {
            Grid gd = new Grid();
            gd.ShowGridLines = true;
            for (int i = 0; i < 8; i++)
            {
                RowDefinition rowDef = new RowDefinition();
                gd.RowDefinitions.Add(rowDef);
                ColumnDefinition colDef = new ColumnDefinition();
                gd.ColumnDefinitions.Add(colDef);
            }

            for (int row = 0; row < 8; row++)
            {
                var colImgsList = imgsList[row];
                for (int col = 0; col < 8; col++)
                {
                    System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                    img.Stretch = Stretch.Fill;
                    Grid.SetRow(img, row);
                    Grid.SetColumn(img, col);
                    img.Source=new BitmapImage(new Uri(colImgsList[col], UriKind.RelativeOrAbsolute));
                    gd.Children.Add(img); 
                }
            }
            this.Content = gd;
        }

        private void SplitBigPicture()
        {
            string imgUrl = @"../../Images/1.jpg";
            int idx = 0;
            if (System.IO.File.Exists(imgUrl))
            {
                Bitmap bp = new Bitmap(imgUrl);
                int originalWidth = bp.Width;
                int originalHeight = bp.Height;
                int newWidth = bp.Width / 8;
                int newHeight = bp.Height / 8;
                string destDir = System.IO.Path.Combine(path, "Split8");
                if (!Directory.Exists(destDir))
                {
                    Directory.CreateDirectory(destDir);
                }
                for (int row = 0; row < 8; row++)
                {
                    List<string> colsImgsList = new List<string>();
                    for (int col = 0; col < 8; col++)
                    {
                        Bitmap newBp = new Bitmap(newWidth, newHeight);
                        using (Graphics g = Graphics.FromImage(newBp))
                        {
                            Rectangle srcRect = new System.Drawing.Rectangle(0 + col * newWidth, 0 + row * newHeight, newWidth, newHeight);
                            g.DrawImage(bp, 0, 0, srcRect, GraphicsUnit.Pixel);
                        }
                        string newFileName = System.IO.Path.Combine(destDir, $"{row}_{col}.jpg");
                        colsImgsList.Add(newFileName);
                        newBp.Save(newFileName);
                    }
                    imgsList.Add(colsImgsList);
                }
            }
        }
    }
}

 

标签:picture,show,C#,System,Windows,using,new,col,row
From: https://www.cnblogs.com/Fred1987/p/18382406

相关文章

  • c++教程之三大结构
    C++顺序结构教程在编程的世界里,顺序结构是构建所有程序的基础。无论是简单的脚本还是复杂的应用程序,它们都是由一系列按照特定顺序执行的指令组成的。C++,作为一种高效、灵活的编程语言,同样遵循这一原则。本教程将深入介绍C++中的顺序结构,包括变量与数据类型、运算符与表达式、......
  • 【JUC并发编程系列】深入理解Java并发机制:CAS算法与原子类在Java中的实践应用(二、CAS
    文章目录【JUC并发编程系列】深入理解Java并发机制:CAS算法与原子类在Java中的实践应用(二、CAS)1.同步之原子类(Atomic类)2.使用atomicInteger计数3.使用atomicInteger底层原理3.compareAndSet原理分析3.1手写AtomicInteger3.2手写Lock锁3.3CASaba的问题3.4Atomic......
  • 【JUC并发编程系列】深入理解Java并发机制:从用户态到内核态的探索(一、前置知识)
    文章目录【JUC并发编程系列】深入理解Java并发机制:从用户态到内核态的探索(一、前置知识)1.用户态与内核态区别2.线程安全同步的方式3.传统锁有哪些缺点4.发生CPU上下文切换的原因5.如何避免上下文切换6.详细总结6.1用户态与内核态6.2线程安全同步方式6.3传统锁的......
  • vue2的el-select虚拟下拉
    说明vue3的的el-select,ElementPlus封装了虚拟下拉的写法,但是有些vue2版本的el-select却没办法用虚拟下拉。如果下拉列表数据量大的话,很导致极度卡顿。那我们就自己封装一个吧代码components下新建elselectV2文件夹,新建两个文件:elOptionNode.vue和VirtualSelect.vue。el......
  • 基于OpenCV-Python实现人脸识别-----摄像头捕获人脸图像显示中文乱码问题
    基于OpenCV-Python实现人脸识别时,为了使图像上显示识别到人员的中文名字,做了几次尝试,使用PIL.Image和OpenCV图像格式相互转换解决:使用OpenCV将图片灰度化,对加载的灰度化图使用分类器中的detectMultiScale()函数查找目标人脸,并使用for循环实现矩形框和圆形框框住查找到的人脸。......
  • 【项目实践】CompletableFuture异步编排在多任务并行执行中的使用
    【项目实践】CompletableFuture异步编排在多任务并行执行中的使用一、单次请求处理多任务的场景        在实际项目中,我们经常会遇到一些比较复杂的查询,需要给前端响应一个内容量较大的响应结果。例如在租房系统的app中,点击具体的某个房间查看详情,需要后端将这个房间的......
  • C# split big picture into small pieces via graphics
    usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;using......
  • vue-cli中webpack-chain 与 configureWebpack 常用配置
    vue-cli文档1.webpack-chain1.定义通过链式调用操作webpack配置对象。(chain:链子)直接修改配置对象chainWebpack通过链式编程的形式,来修改默认的webpack配置2.包含的对象ChainedMap和ChainSet1.ChainedMap//1、从Map移除所有配置clear()//2、通过键值从Map......
  • OpenCV(VS2022配置OpenCV开发环境)
    目录1.下载OpenCV2.添加环境变量3.添加项目属性表4.配置DeBug属性表5.新的项目中快速配置6.配置Release属性表1.下载OpenCV访问:https://opencv.org/releases/2.添加环境变量添加环境变量%opencv%\build\x64\vc15\bin其中%opencv%为你自己的opencv文件夹的位置。......
  • Docker Compose配置详解
    1.什么是DockerCompose?DockerCompose是一种用于定义和运行多容器Docker应用程序的工具。通过一个docker-compose.yml文件,你可以配置应用程序的所有服务(例如,Web服务器、数据库、缓存)并轻松管理它们。2.基本DockerCompose命令docker-composeup:启动并运行docker-compose.ym......