private void btnSprite_Click(object sender, EventArgs e) { if (!OpenFile(true)) { return; } DialogResult dr = openFileDialog.ShowDialog(); if (DialogResult.OK == dr && openFileDialog.FileNames.Length > 0) { basePath = Path.GetDirectoryName(openFileDialog.FileName); folderBrowserDialog.SelectedPath = basePath; var spriteFile = new SpriteFile(); try { spriteFile = (SpriteFile)XmlSerializer.LoadFromXml(openFileDialog.FileNames[0], spriteFile.GetType()); if (_imgList == null) { _imgList = new List <ImageInfo>(); } var noFile = "这些文件不存在:" + Environment.NewLine; var hasFile = false; foreach (Sprite s in spriteFile.SpriteList) { var path = folderBrowserDialog.SelectedPath + "\\" + s.Path; if (File.Exists(path)) { Image img = Image.FromFile(path); string imgName = Path.GetFileNameWithoutExtension(s.Path); ImageInfo imgInfo = new ImageInfo(img, imgName, path); img.Tag = imgInfo; _imgList.Add(imgInfo); AddPictureBox(img, s.LocationX, s.LocationY); } else { hasFile = true; noFile += path + Environment.NewLine; } } if (hasFile) { MessageBox.Show(noFile, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } txtDir.Text = spriteFile.CssFileName; txtName.Text = spriteFile.ImageName; chkBoxPhone.Checked = spriteFile.IsPhone; comboBoxImgType.Text = spriteFile.SpriteImgFileType == null ? "png" : spriteFile.SpriteImgFileType; panelImages.ResumeLayout(false); SetCssText(); SetBase64(); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ".sprite文件被损坏,无法打开!"); } } }
private void ButtonMakeBigImageCss_Click(object sender, EventArgs e) { panelImages.VerticalScroll.Value=0 ; panelImages.HorizontalScroll.Value = 0; if (_imgList == null || _imgList.Count < 2) { MessageBox.Show("请选择多个背景图片。"); return; } DialogResult dr = folderBrowserDialog.ShowDialog(); if (dr == DialogResult.OK) { string imgDir = folderBrowserDialog.SelectedPath; if (!Directory.Exists(imgDir)) { Directory.CreateDirectory(imgDir); } string imgPath = Path.Combine(imgDir, txtName.Text+"."+GetImgExt()); if (File.Exists(imgPath)) { if (DialogResult.Yes != MessageBox.Show("选定文件夹中已存在" + txtName.Text + "." + GetImgExt() + ",继续执行将覆盖已存在文件,是否继续?", "询问" , MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { return; } } int maxWidth,maxHeight,minWidth,minHeight; maxWidth = maxHeight = minWidth = minHeight = 0; //循环获取距离左边和上边最小距离 //把所有元素按照0,0点为标准,通过最小向上距离和向左距离平移,获取最大距离 foreach (PictureBox pb in panelImages.Controls) { if (panelImages.Controls.GetChildIndex(pb) == 0) { minWidth = pb.Location.X; minHeight = pb.Location.Y; } minWidth = Math.Min(minWidth, pb.Location.X); minHeight = Math.Min(minHeight, pb.Location.Y); maxWidth = Math.Max(maxWidth, pb.Location.X + pb.Image.Width); maxHeight = Math.Max(maxHeight, pb.Location.Y + pb.Image.Height); } Size imgSize = new Size(maxWidth, maxHeight); //var codeMime = string.Empty; using (Bitmap bigImg = new Bitmap(imgSize.Width-minWidth, imgSize.Height-minHeight, PixelFormat.Format32bppArgb)) { string imgType = GetImgExt(); ImageFormat format = ImageFormat.Png; switch (imgType) { case "jpeg": format = ImageFormat.Jpeg; break; case "jpg": format = ImageFormat.Jpeg; break; case "png": format = ImageFormat.Png; break; default: break; } using (Graphics g = Graphics.FromImage(bigImg)) { //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; if ((format == ImageFormat.Jpeg)) g.Clear(Color.White); else g.Clear(Color.Transparent); SetCssText(); SetBase64(); var sprite = new SpriteFile() { CssFileName = txtDir.Text, ImageName = txtName.Text, SpriteList = new List<Sprite>(), IsPhone = chkBoxPhone.Checked, SpriteImgFileType = comboBoxImgType.Text }; try { foreach (PictureBox pb in panelImages.Controls) { var img = (ImageInfo)pb.Image.Tag; var path = img.FileName; Sprite s = new Sprite() { LocationY = pb.Location.Y, LocationX = pb.Location.X, Path = Path.GetFileName(path) }; sprite.SpriteList.Add(s); g.DrawImage(pb.Image, pb.Location.X - minWidth, pb.Location.Y - minHeight, pb.Image.Width, pb.Image.Height); if (Path.GetDirectoryName(path) != folderBrowserDialog.SelectedPath) { File.Copy(path, folderBrowserDialog.SelectedPath + "\\" + Path.GetFileName(path), false); } } XmlSerializer.SaveToXml(folderBrowserDialog.SelectedPath + "\\" + txtName.Text + ".sprite", sprite); } catch (Exception ex) { MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Error); return; } } try { //保存图片 bigImg.Save(imgPath, format); MessageBox.Show("图片生成成功!"); } catch (Exception ex) { MessageBox.Show(ex.Message+"图片生成失败,被覆盖文件可能被其他程序占用,请换个文件名!"); } } } }
private void btnSprite_Click(object sender, EventArgs e) { if (!OpenFile(true)) { return; } DialogResult dr = openFileDialog.ShowDialog(); if (DialogResult.OK == dr && openFileDialog.FileNames.Length > 0) { basePath = Path.GetDirectoryName(openFileDialog.FileName); folderBrowserDialog.SelectedPath = basePath; var spriteFile=new SpriteFile(); try { spriteFile = (SpriteFile)XmlSerializer.LoadFromXml(openFileDialog.FileNames[0], spriteFile.GetType()); if (_imgList == null) { _imgList = new List<ImageInfo>(); } var noFile = "这些文件不存在:" + Environment.NewLine; var hasFile=false; foreach (Sprite s in spriteFile.SpriteList) { var path=folderBrowserDialog.SelectedPath+"\\"+ s.Path; if (File.Exists(path)) { Image img = Image.FromFile(path); string imgName = Path.GetFileNameWithoutExtension(s.Path); ImageInfo imgInfo = new ImageInfo(img, imgName, path); img.Tag = imgInfo; _imgList.Add(imgInfo); AddPictureBox(img, s.LocationX, s.LocationY); } else { hasFile=true; noFile += path + Environment.NewLine; } } if (hasFile) { MessageBox.Show(noFile, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } txtDir.Text = spriteFile.CssFileName; txtName.Text = spriteFile.ImageName; chkBoxPhone.Checked = spriteFile.IsPhone; comboBoxImgType.Text = spriteFile.SpriteImgFileType == null ? "png" : spriteFile.SpriteImgFileType; panelImages.ResumeLayout(false); SetCssText(); SetBase64(); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ".sprite文件被损坏,无法打开!"); } } }
private void ButtonMakeBigImageCss_Click(object sender, EventArgs e) { panelImages.VerticalScroll.Value = 0; panelImages.HorizontalScroll.Value = 0; if (_imgList == null || _imgList.Count < 2) { MessageBox.Show("请选择多个背景图片。"); return; } DialogResult dr = folderBrowserDialog.ShowDialog(); if (dr == DialogResult.OK) { string imgDir = folderBrowserDialog.SelectedPath; if (!Directory.Exists(imgDir)) { Directory.CreateDirectory(imgDir); } string imgPath = Path.Combine(imgDir, txtName.Text + "." + GetImgExt()); if (File.Exists(imgPath)) { if (DialogResult.Yes != MessageBox.Show("选定文件夹中已存在" + txtName.Text + "." + GetImgExt() + ",继续执行将覆盖已存在文件,是否继续?", "询问" , MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { return; } } int maxWidth, maxHeight, minWidth, minHeight; maxWidth = maxHeight = minWidth = minHeight = 0; //循环获取距离左边和上边最小距离 //把所有元素按照0,0点为标准,通过最小向上距离和向左距离平移,获取最大距离 foreach (PictureBox pb in panelImages.Controls) { if (panelImages.Controls.GetChildIndex(pb) == 0) { minWidth = pb.Location.X; minHeight = pb.Location.Y; } minWidth = Math.Min(minWidth, pb.Location.X); minHeight = Math.Min(minHeight, pb.Location.Y); maxWidth = Math.Max(maxWidth, pb.Location.X + pb.Image.Width); maxHeight = Math.Max(maxHeight, pb.Location.Y + pb.Image.Height); } Size imgSize = new Size(maxWidth, maxHeight); //var codeMime = string.Empty; using (Bitmap bigImg = new Bitmap(imgSize.Width - minWidth, imgSize.Height - minHeight, PixelFormat.Format32bppArgb)) { string imgType = GetImgExt(); ImageFormat format = ImageFormat.Png; switch (imgType) { case "jpeg": format = ImageFormat.Jpeg; break; case "jpg": format = ImageFormat.Jpeg; break; case "png": format = ImageFormat.Png; break; default: break; } using (Graphics g = Graphics.FromImage(bigImg)) { //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; if ((format == ImageFormat.Jpeg)) { g.Clear(Color.White); } else { g.Clear(Color.Transparent); } SetCssText(); SetBase64(); var sprite = new SpriteFile() { CssFileName = txtDir.Text, ImageName = txtName.Text, SpriteList = new List <Sprite>(), IsPhone = chkBoxPhone.Checked, SpriteImgFileType = comboBoxImgType.Text }; try { foreach (PictureBox pb in panelImages.Controls) { var img = (ImageInfo)pb.Image.Tag; var path = img.FileName; Sprite s = new Sprite() { LocationY = pb.Location.Y, LocationX = pb.Location.X, Path = Path.GetFileName(path) }; sprite.SpriteList.Add(s); g.DrawImage(pb.Image, pb.Location.X - minWidth, pb.Location.Y - minHeight, pb.Image.Width, pb.Image.Height); if (Path.GetDirectoryName(path) != folderBrowserDialog.SelectedPath) { File.Copy(path, folderBrowserDialog.SelectedPath + "\\" + Path.GetFileName(path), false); } } XmlSerializer.SaveToXml(folderBrowserDialog.SelectedPath + "\\" + txtName.Text + ".sprite", sprite); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } try { //保存图片 bigImg.Save(imgPath, format); MessageBox.Show("图片生成成功!"); } catch (Exception ex) { MessageBox.Show(ex.Message + "图片生成失败,被覆盖文件可能被其他程序占用,请换个文件名!"); } } } }