bmp转换png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Dictionary<string, string> dic = new Dictionary<string, string>();
bool bEffect = true;
private void Form1_Load(object sender, EventArgs e)
{
}

public Form1()
{
InitializeComponent();
bEffect = true;
ListFiles(new DirectoryInfo("f:/www5"));
}


public void modifyToPng(string sd, string sf)
{
Image srcImage = Image.FromFile(sf);
int resW = srcImage.Width;
int resH = srcImage.Height;
Bitmap resultImage = new Bitmap(resW, resH);
Graphics gg = Graphics.FromImage(resultImage);
gg.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
gg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gg.DrawImage(srcImage, new Rectangle(0, 0, resW, resH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);


for (int i = 0; i < resW; i++)
{
for (int j = 0; j < resH; j++)
{
Color c = resultImage.GetPixel(i, j);
if (bEffect)
{
//透明度必须超过rgb最大差值
int dif1 = Math.Abs(c.R - c.G);
int dif2 = Math.Abs(c.R - c.B);
int dif3 = Math.Abs(c.G - c.B);
int maxDiff = Math.Max(dif1, Math.Max(dif2, dif3));

int a = (int)(0.2989 * c.R + 0.5866 * c.G + 0.1145 * c.B);
if (a > 255) a = 255;
if (a < maxDiff) a = maxDiff;

//保持颜色比例
int max = Math.Max(c.R, Math.Max(c.G, c.B));
if (max == 0) max = 1;
int rateR = 255 * c.R / max;
int rateG = 255 * c.G / max;
int rateB = 255 * c.B / max;

int r = c.R * 255 / (a == 0 ? 1 : a);
if (r > rateR) r = rateR;
int g = c.G * 255 / (a == 0 ? 1 : a);
if (g > rateG) g = rateG;
int b = c.B * 255 / (a == 0 ? 1 : a);
if (b > rateB) b = rateB;
resultImage.SetPixel(i, j, Color.FromArgb(a, r, g, b));
}else{
int a = (c.R == 0 && c.G == 0 && c.B == 0) ? 0 : 255;
resultImage.SetPixel(i, j, Color.FromArgb(a, c));
}
}
}
resultImage.Save(sf.Substring(0, sf.LastIndexOf(".")) + ".png", System.Drawing.Imaging.ImageFormat.Png);
}

public void ListFiles(FileSystemInfo info)
{
if (!info.Exists) return;

DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null) return;

FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//是文件
if (file != null)
{
//Console.WriteLine(file.FullName + "\t " + file.Length);
if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".bmp")
//此处为显示JPG格式,不加IF可遍历所有格式的文件
{
//this.list1.Items.Add(file);
//MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
modifyToPng(file.DirectoryName, file.FullName);
}
}
//对于子目录,进行递归调用
else
{
ListFiles(files[i]);
}

}

}
}
}

转载于:https://www.cnblogs.com/agchuanqi/p/7857878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值