c#修改图片尺寸不改变原有图片比例
c#使用bitmap修改图片尺寸
修改图片大小,不改变原有图片比例
修改后图片
代码
public static void image() { system.drawing.image img = system.drawing.image.fromfile("图片路径"); bitmap bt = new bitmap(img); //获取图片位置颜色 color cl = bt.getpixel(10, 10); int width = img.width; int height = img.height; int marginx = height / 2; float dpix = img.horizontalresolution; float dpiy = img.verticalresolution; //设置新图的大小 bitmap bitmap= new bitmap(width, width, pixelformat.format24bpprgb); //设置位图文件的水平和垂直分辨率,与img一致 bitmap.setresolution(dpix, dpiy); //在位图文件上填充一个新图 graphics graphics = graphics.fromimage(bitmap); system.drawing.rectangle rec = new system.drawing.rectangle(0, 0, width, width); //定义颜色 solidbrush mysolidbrush = new solidbrush(cl); //将新图填充为获取原图位置的颜色 graphics.fillrectangle(mysolidbrush, rec); //向新图中填充img graphics.drawimage(img, 0, marginx, rec, graphicsunit.pixel); graphics.dispose(); gc.collect(); bitmap.save("保存图片路径", system.drawing.imaging.imageformat.jpeg); }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论