using system;
using system.collections.generic;
using system.text;
using system.io;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;namespace pub.class {
///
/// 缩图处理类
///
public class cutimage
{
#region 构造函数
public cutimage() { }
#endregion#region 私有成员
private int width;
private int height;
private int size;
private string errmsg;
#endregion#region 公共属性
///
/// 图片宽度
///
public int width { get { return width; } set { width = value; } }
///
/// 图片高度
///
public int height { get { return height; } set { height = value; } }
///
/// 图片尺寸
///
public int size { get { return size; } set { size = value; } }
///
/// 错误消息
///
public string errmsg { get { return this.errmsg; } set { this.errmsg = value; } }
#endregion#region 公共方法
///
/// 获取图片信息
///
/// 图片地址
///成功true失败false
public bool getimage(string imagepath) {
try {
system.drawing.image image = system.drawing.image.fromfile(imagepath);
this.width = image.width;
this.height = image.height;
image.dispose();filestream fs = new filestream(imagepath, filemode.open, fileaccess.read);
this.size = (int)fs.length;
fs.close();return true;
} catch (exception ex) {
this.errmsg = ex.tostring();
return false;
}
}///
/// 按宽x长比例切图
///
/// 源图地址
/// 新图地址
/// 宽度
/// 高度
///成功true失败false
public bool cutimagecustommin(string imagepath, string savepath, int cutwidth, int cutheight) {
try {
system.drawing.image objimage = system.drawing.image.fromfile(imagepath);
float x = objimage.width;
float y = objimage.height;float xpercent = x / cutwidth;
float ypercent = y / cutheight;if (xpercent < ypercent) {
this.width = (int)((x * cutheight) / y);
this.height = cutheight;
} else {
this.width = cutwidth;
this.height = (int)((cutwidth * y) / x);
}system.drawing.image newimage = new bitmap(objimage.width, objimage.height, pixelformat.format32bpprgb);
graphics g = graphics.fromimage(newimage);
g.drawimage(objimage, 0, 0, objimage.width, objimage.height);
g.dispose();
system.drawing.image thumbimage = newimage.getthumbnailimage(this.width, this.height, null, intptr.zero);
thumbimage.save(savepath, objimage.rawformat);objimage.dispose();
newimage.dispose();
thumbimage.dispose();filestream fs = new filestream(savepath, filemode.open, fileaccess.read);
this.size = (int)fs.length;
fs.close();return true;
} catch (exception ex) {
this.errmsg = ex.tostring();
return false;
}
}///
/// 按宽x长比例切图
///
/// 源图地址
/// 新图地址
/// 宽度
/// 高度
///成功true失败false
public bool cutimagecustom(string imagepath, string savepath, int cutwidth, int cutheight) {
try {
system.drawing.image objimage = system.drawing.image.fromfile(imagepath);
float x = objimage.width;
float y = objimage.height;float xpercent = x / cutwidth;
float ypercent = y / cutheight;if (xpercent < ypercent) {
this.width = (int)((x * cutheight) / y);
this.height = cutheight;
} else {
this.width = cutwidth;
this.height = (int)((cutwidth * y) / x);
}bitmap newimage = new bitmap(this.width, this.height, pixelformat.format32bpprgb);
newimage.setresolution(72f, 72f);
graphics gdiobj = graphics.fromimage(newimage);
gdiobj.compositingquality = compositingquality.highquality;
gdiobj.smoothingmode = smoothingmode.highquality;
gdiobj.interpolationmode = interpolationmode.highqualitybicubic;
gdiobj.pixeloffsetmode = pixeloffsetmode.highquality;
gdiobj.fillrectangle(new solidbrush(color.white), 0, 0, this.width, this.height);
rectangle destrect = new rectangle(0, 0, this.width, this.height);
gdiobj.drawimage(objimage, destrect, 0, 0, objimage.width, objimage.height, graphicsunit.pixel);
gdiobj.dispose();system.drawing.imaging.encoderparameters ep = new system.drawing.imaging.encoderparameters(1);
ep.param[0] = new system.drawing.imaging.encoderparameter(system.drawing.imaging.encoder.quality, (long)100);imagecodecinfo[] codecs = imagecodecinfo.getimageencoders();
imagecodecinfo ici = null;
foreach (imagecodecinfo codec in codecs) {
if (codec.mimetype == "image/jpeg") { ici = codec; }
}if (ici != null) newimage.save(savepath, ici, ep); else newimage.save(savepath, objimage.rawformat);
objimage.dispose();
newimage.dispose();filestream fs = new filestream(savepath, filemode.open, fileaccess.read);
this.size = (int)fs.length;
fs.close();return true;
} catch (exception ex) {
this.errmsg = ex.tostring();
return false;
}
}///
/// 将图片缩放到指定的宽度
///
/// 源图地址
/// 新图地址
/// 宽度
///成功true失败false
public bool cutimagebywidth(string imagepath, string savepath, int square) {
try {
int cutwidth = square;system.drawing.image objimage = system.drawing.image.fromfile(imagepath);
float x = objimage.width;
float y = objimage.height;this.width = cutwidth;
this.height = (int)((cutwidth * y) / x);system.drawing.image newimage = new bitmap(objimage.width, objimage.height, pixelformat.format32bpprgb);
graphics g = graphics.fromimage(newimage);
g.drawimage(objimage, 0, 0, objimage.width, objimage.height);
g.dispose();
system.drawing.image thumbimage = newimage.getthumbnailimage(this.width, this.height, null, intptr.zero);
thumbimage.save(savepath, objimage.rawformat);objimage.dispose();
newimage.dispose();
thumbimage.dispose();filestream fs = new filestream(savepath, filemode.open, fileaccess.read);
this.size = (int)fs.length;
fs.close();return true;
} catch (exception ex) {
this.errmsg = ex.tostring();
return false;
}
}///
/// 将图片缩放到指定的高度
///
/// 源图地址
/// 新图地址
/// 高度
///成功true失败false
public bool cutimagebyheight(string imagepath, string savepath, int square) {
try {
int cutheight = square;system.drawing.image objimage = system.drawing.image.fromfile(imagepath);
float x = objimage.width;
float y = objimage.height;this.height = cutheight;
this.width = (int)((cutheight * x) / y);system.drawing.image newimage = new bitmap(objimage.width, objimage.height, pixelformat.format32bpprgb);
graphics g = graphics.fromimage(newimage);
g.drawimage(objimage, 0, 0, objimage.width, objimage.height);
g.dispose();
system.drawing.image thumbimage = newimage.getthumbnailimage(this.width, this.height, null, intptr.zero);
thumbimage.save(savepath, objimage.rawformat);objimage.dispose();
newimage.dispose();
thumbimage.dispose();filestream fs = new filestream(savepath, filemode.open, fileaccess.read);
this.size = (int)fs.length;
fs.close();return true;
} catch (exception ex) {
this.errmsg = ex.tostring();
return false;
}
}///
/// 将图片剪切到一个正方形
///
/// 源图地址
/// 新图地址
/// 正方形边长
///成功true失败false
public bool cutimagesquare(string imagepath, string savepath, int square) {
try {
this.width = square;
this.height = square;
int cutwidth = square;
int cutheight = square;system.drawing.image objimage = system.drawing.image.fromfile(imagepath);
if (objimage.width >= objimage.height) {
cutwidth = objimage.height;
cutheight = objimage.height;
} else {
cutwidth = objimage.width;
cutheight = objimage.width;
}system.drawing.image newimage = new bitmap(cutwidth, cutheight, pixelformat.format32bpprgb);
graphics g = graphics.fromimage(newimage);
g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;rectangle destrect = new rectangle(0, 0, cutwidth, cutheight);
rectangle srcrect = new rectangle(0, 0, cutwidth, cutheight);
graphicsunit units = graphicsunit.pixel;g.drawimage(objimage, destrect, srcrect, units);
g.dispose();
system.drawing.image thumbimage = newimage.getthumbnailimage(this.width, this.height, null, intptr.zero);
thumbimage.save(savepath, objimage.rawformat);objimage.dispose();
newimage.dispose();
thumbimage.dispose();filestream fs = new filestream(savepath, filemode.open, fileaccess.read);
this.size = (int)fs.length;
fs.close();return true;
} catch (exception ex) {
this.errmsg = ex.tostring();
return false;
}
}#region 处理图片长宽显示
public static void getpropersize(int truewidth, int trueheight, int placewidth, int placeheight, out int showwidth, out int showheight) {
if (trueheight < placeheight && truewidth < placewidth) {
showheight = trueheight;
showwidth = truewidth;
} else {
float x = (float)truewidth;
float y = (float)trueheight;float xpercent = x / placewidth;
float ypercent = y / placeheight;if (xpercent < ypercent) {
我的博物馆故事 官方安卓版v1.61.2
我的博物馆故事是一款以消除为主题的经营养成类手游,在这里玩家
专业模拟飞行10 手机版v12.2.4
专业模拟飞行10安卓版是一款飞行休闲手游,顶尖的物理飞行引擎
动物起义战斗模拟器二琳同款 最新版v4.1.1
动物起义战斗模拟器是一个非常有趣的模拟类游戏,玩家可以召唤各
迷你世界七周年 安卓手机版v1.43.0
迷你世界7周年是一款由《迷你世界》官方推出的庆祝特别版本,在
劫后公司无限资源版 v1.0.5.1
劫后公司内置菜单版是游戏的破解版本,在该版本中为玩家提供了内