string imgpath = Server.MapPath("\\CommodityImg\\img\\");
string newguid = Guid.NewGuid().ToString();
HttpPostedFile file = files[i];
string extension = System.IO.Path.GetExtension(file.FileName);
调取方法:Editpic(imgpath + newguid + extension, smallimgpath + "small" + newguid + extension, size);
/// <summary>
/// 保存图片
/// </summary>
/// <param name="aimpath">路径</param>
/// <param name="scale">0L-100L</param>
public static void Editpic(string oldfile, string newfile, int bzpicsize)
{
//string originalFilename = sourcepath;
//生成的高质量图片名称
string strGoodFile = newfile;
int width = 0, hight = 0;
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(oldfile);
//新建一个bmp图片
int w = image.Width;
int h = image.Height;
if (w <= h && h > bzpicsize)
{
hight = bzpicsize;
width = (w * bzpicsize) / h;
}
else if (h <= w && w > bzpicsize)
{
width = bzpicsize;
hight = (h * bzpicsize) / w;
}
else
{
width = w;
hight = h;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, hight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Transparent);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;//获取品质(压缩率)编码
EncoderParameter mycoder = new EncoderParameter(encoder, 100L);//0压缩率最大,100品质最高
EncoderParameters myCoders = new EncoderParameters(1);//参数数组,大小为1
myCoders.Param[0] = mycoder;//添加一个参数
ImageCodecInfo jpgInfo = GetEncoder(ImageFormat.Jpeg);//获取JPG格式编解码信息
Bitmap myBitmap = new Bitmap(bitmap);
for (int i = 0; i < myBitmap.Width; i++)
{
Color Color2 = myBitmap.GetPixel(i, 1); //调用GetPixel方法获取像素点的颜色
double red2 = Color2.R;
double green2 = Color2.G;
double blue2 = Color2.B;
myBitmap.SetPixel(i, 0, Color.FromArgb((int)red2, (int)green2, (int)blue2));
}
for (int i = 0; i < myBitmap.Height; i++)
{
Color Color2 = myBitmap.GetPixel(1, i); //调用GetPixel方法获取像素点的颜色
double red2 = Color2.R;
double green2 = Color2.G;
double blue2 = Color2.B;
myBitmap.SetPixel(0, i, Color.FromArgb((int)red2, (int)green2, (int)blue2)); //用SetPixel()方法设置像素点的颜色
}
try
{
//以jpg格式保存缩略图
bitmap.Save(strGoodFile);
}
catch (System.Exception e)
{
throw e;
}
finally
{
image.Dispose();
bitmap.Dispose();
g.Dispose();
//bitmap.MakeTransparent(Color.Transparent);
}
}
private static ImageCodecInfo GetEncoder(ImageFormat format)//获取特定的图像编解码信息
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}