123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Media.Imaging;
- namespace PDF_Master.Helper
- {
- public class PictureConverter
- {
- /// <summary>
- /// 图片转PNG(其他图片先转为PNG在转JPG)
- /// picturefile 图片文件名
- /// filePath 保存路径例如//filePath.png
- /// </summary>
- public static bool SavePng(string picturefile, string filePath)
- {
- Image img = Image.FromFile(picturefile);
- try
- {
- using (var bmp = new Bitmap(img.Width, img.Height))
- {
- bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
- using (var g = Graphics.FromImage(bmp))
- {
- g.Clear(Color.White);
- g.DrawImageUnscaled(img, 0, 0);
- }
- bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 图片转PNG(其他图片先转为PNG在转JPG)
- /// picturefile 图片image
- /// filePath 保存路径例如//filePath.png
- /// </summary>
- public static bool SavePng(Image img, string filePath)
- {
- try
- {
- using (var bmp = new Bitmap(img.Width, img.Height))
- {
- bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
- using (var g = Graphics.FromImage(bmp))
- {
- g.Clear(Color.White);
- g.DrawImageUnscaled(img, 0, 0);
- }
- bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 图片转JPeG
- /// picturefile 图片文件名
- /// filePath 保存路径例如//filePath.jpg
- /// </summary>
- public static bool SaveJpeg(string picturefile, string filePath)
- {
- Image img = Image.FromFile(picturefile);
- try
- {
- using (var bmp = new Bitmap(img.Width, img.Height))
- {
- bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
- using (var g = Graphics.FromImage(bmp))
- {
- g.Clear(Color.White);
- g.DrawImageUnscaled(img, 0, 0);
- }
- //存储各种格式
- //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
- //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
- bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 图片转JPeG
- /// img 图片Image
- /// filePath 保存路径例如//filePath.jpg
- /// </summary>
- public static bool SaveJpeg(Image img, string filePath)
- {
-
- try
- {
- using (var bmp = new Bitmap(img.Width, img.Height))
- {
- bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
- using (var g = Graphics.FromImage(bmp))
- {
- g.Clear(Color.White);
- g.DrawImageUnscaled(img, 0, 0);
- }
- //存储各种格式
- //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
- //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
- bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- public static byte[] GetImageByteFromPath(string ImagePath, string fileExt, out double width, out double height)
- {
- try
- {
- #region 跟图章一样的转换方法。
- if (ImagePath != null && ImagePath.Length > 0 && File.Exists(ImagePath))
- {
- FileStream fileData = File.OpenRead(ImagePath);
- BitmapFrame frame = null;
- width = 0;
- height = 0;
- if (fileExt.Contains(".jpg") || fileExt.Contains(".jpeg"))
- {
- JpegBitmapDecoder decoder = (JpegBitmapDecoder)JpegBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
- frame = decoder.Frames[0];
- width = frame.Width;
- height = frame.Height;
- }
- else if (fileExt.Contains(".png"))
- {
- PngBitmapDecoder decoder = (PngBitmapDecoder)PngBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
- frame = decoder.Frames[0];
- width = frame.Width;
- height = frame.Height;
- }
- else if (fileExt.Contains(".bmp"))
- {
- BmpBitmapDecoder decoder = (BmpBitmapDecoder)BmpBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
- frame = decoder.Frames[0];
- width = frame.Width;
- height = frame.Height;
- }
- else//默认采用png的解码方式
- {
- BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
- frame = decoder.Frames[0];
- width = frame.Width;
- height = frame.Height;
- }
- if (frame != null)
- {
- var ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
- return ImageArray;
- }
- return null;
- }
- width = 0;
- height = 0;
- return null;
- #endregion
- }
- catch
- {
- width = 0;
- height = 0;
- return null;
- }
- }
- }
- }
|