首页 > 学院 > 开发设计 > 正文

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

2019-11-06 06:50:59
字体:
来源:转载
供稿:网友
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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表