123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PDF_Office.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;
- }
- }
- }
- }
|