主角怪物转换去除黑底并输出xy

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 WindowsFormsApp1
{
public partial class Form1 : Form
{
bool bEffect = false;//是否是特效
bool heibian = true;//是否有黑边
int startNum = 0;//前起始编号
string before = "ddny";//后前缀编号
bool bHero = false;//是否玩家
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

int[] arrayHero = { 0, 1, 2, 3,
8, 9, 10, 11,
16, 17, 18, 19,
24, 25, 26, 27,
32, 33, 34, 35,
40, 41, 42, 43,
48, 49, 50, 51,
56, 57, 58, 59,
128,129,130,131,132,133,
136,137,138,139,140,141,
144,145,146,147,148,149,
152,153,154,155,156,157,
160,161,162,163,164,165,
168,169,170,171,172,173,
176,177,178,179,180,181,
184,185,186,187,188,189,

200,201,202,203,204,205,
208,209,210,211,212,213,
216,217,218,219,220,221,
224,225,226,227,228,229,
232,233,234,235,236,237,
240,241,242,243,244,245,
248,249,250,251,252,253,
256,257,258,259,260,261,

392,393,394,395,396,397,
400,401,402,403,404,405,
408,409,410,411,412,413,
416,417,418,419,420,421,
424,425,426,427,428,429,
432,433,434,435,436,437,
440,441,442,443,444,445,
448,449,450,451,452,453,

192,193,194,195,196,197,198,199,
};

int[] arrayMonster = { 0, 1, 2, 3
,10, 11, 12, 13
,20, 21, 22, 23
,30, 31, 32, 33
,40, 41, 42, 43
,50, 51, 52, 53
,60, 61, 62, 63
,70, 71, 72, 73

,80, 81, 82, 83,84,85
,90, 91, 92, 93,94,95
,100, 101, 102, 103,104,105
,110, 111, 112, 113,114,115
,120, 121, 122, 123,124,125
,130, 131, 132, 133,134,135
,140, 141, 142, 143,144,145
,150, 151, 152, 153,154,155

,160, 161, 162, 163,164,165
,170, 171, 172, 173,174,175
,180, 181, 182, 183,184,185
,190, 191, 192, 193,194,195
,200, 201, 202, 203,204,205
,210, 211, 212, 213,214,215
,220, 221, 222, 223,224,225
,230, 231, 232, 233,234,235
};

int[] array = bHero ? arrayHero : arrayMonster;

//其他情况
//int[] arrayOther = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27};
//int[] arrayOther = {
// 100, 101, 102, 103,104,105
// ,120, 121, 122, 123,124,125
// ,140, 141, 142, 143,144,145
// ,160, 161, 162, 163,164,165
// ,180, 181, 182, 183,184,185
// ,200, 201, 202, 203,204,205
// ,220, 221, 222, 223,224,225
// ,240, 241, 242, 243,244,245
//};
//int[] arrayOther = { 0, 1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
//array = arrayOther;

if (!Directory.Exists("E:/resource/monbx/" + before + "--/"))
{
Directory.CreateDirectory("E:/resource/monbx/" + before + "--/");
}
string result = "";
for (int i = 0; i < array.Length; ++i)
{
string str = "" + (startNum + array[i]);
while (str.Length < 5) str = "0" + str;
String namenew = "" + i;
while (namenew.Length < 3) namenew = "0" + namenew;
namenew = before + namenew;
string path1 = "E:/resource/monbx/" + before + "/" + str + ".PNG";
string path2 = "E:/resource/monbx/" + before + "--/" + namenew + ".png";
if (heibian)
{
modifyToPng(path1, path2);
}
else
{
System.IO.File.Copy(path1, path2, true);
}

 

Image srcImage = Image.FromFile(path1);
string fileContent = File.ReadAllText("E:/resource/monbx/" + before + "/Placements/" + str + ".txt");
int lastdot = fileContent.IndexOf("\r");
int a = int.Parse(fileContent.Substring(0, lastdot));
int b = int.Parse(fileContent.Substring(lastdot + 2));
double aa = a - 24 + srcImage.Width * 0.5;
double bb = 20 - b - srcImage.Height * 0.5;

result += "\"" + namenew + "\" : \"" + aa + "," + bb + "\",\n";
}
File.AppendAllText("E:/resource/monbx/" + before + "--/pos.txt", result);
}


public void modifyToPng(string sf, string df)
{
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) * 1.5);
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, Math.Min(r + 0, 255), Math.Min(g + 0, 255), Math.Min(b + 0, 255)));
}
else
{
int a = (c.R == 0 && c.G == 0 && c.B == 0) ? 0 : 255;
resultImage.SetPixel(i, j, Color.FromArgb(a, c));
}
}
}
resultImage.Save(df, System.Drawing.Imaging.ImageFormat.Png);
}
}
}

posted on 2018-09-28 01:07  agchuanqi  阅读(171)  评论(0编辑  收藏  举报