WPF 获取指定目录下的图片文件,然后进行切割

来源:互联网 发布:java安全框架权限管理 编辑:程序博客网 时间:2024/06/06 08:47
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string imgtype = "*.BMP|*.JPG";
            string[] ImageType = imgtype.Split('|');
            for (int i = 0; i < ImageType.Length; i++)
            {
                string[] files = Directory.GetFiles(@"E:\\CJImage", ImageType[i]);
                if (files != null)
                {
                    for (int j = 0; j < files.Length; j++)
                    {
                        string extension = Path.GetExtension(files[j]);
                        if (extension.ToLower() == ".jpg" || extension.ToLower() == ".bmp")
                        {
                            this.imagePath = files[j];
                            FileInfo fileInfo = new FileInfo(files[j]);
                            int iCount = (int)fileInfo.Length / 10000000;
                            int rows = iCount < 1 ? 1 : iCount;
                            int columns = 1;
                            System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath);
                            int sh = img.Height / rows;
                            int sw = img.Width / columns;
                            for (int k = 0; k < rows; k++)
                            {
                                CreateSlice(0, k * sh, sw, sh,Path.Combine(new string[] { @"E:\\CutImage\\", String.Format("{0}{1}x{2}{3}",Path.GetFileNameWithoutExtension(imagePath), k + 1, 1,Path.GetExtension(imagePath)) }));
                            }
                            img.Dispose();
                        }
                    }
                }
            }
            MessageBox.Show("图片切片完成!");
            GC.Collect();
        }


        #region 切片
        /// <summary>
        /// 切片
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="saveTo"></param>
        public void CreateSlice(int x, int y, int width, int height, string saveTo)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath);
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(System.Drawing.Color.Transparent);
            g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(x, y, width, height), System.Drawing.GraphicsUnit.Pixel);
            try
            {
                bitmap.Save(saveTo, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                bitmap.Dispose();
                img.Dispose();
                g.Dispose();
            }
        }
        #endregion
0 0
原创粉丝点击