客户端上传图片到服务器怎么实现自动压缩图片的大小那?? 菜鸟请教。。怎么实现压缩图片大小的功能? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将原图上传到服务器后,用draw把原图重绘。 public Bitmap MakeThumb(System.Drawing.Image OImage, int k, int tw, int th) { int ow = OImage.Width; int oh = OImage.Height; int a = 0; int b = 0; int c = ow; int d = oh; int nw = tw; int nh = th; double x1 = Convert.ToDouble(Convert.ToDouble(ow) / Convert.ToDouble(tw)); double x2 = Convert.ToDouble(Convert.ToDouble(oh) / Convert.ToDouble(th)); switch (k) { case 1://取中心部分 if (x1 > x2) { b = 0; d = oh; c = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(th))); a = (ow - c) / 2; } else { a = 0; c = ow; d = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(tw))); b = (oh - d) / 2; } break; case 2://强制宽度符合缩略图 nh = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(ow))); break; case 3://强制高度符合缩略图 nw = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(oh))); break; case 4://宽度大于缩略图宽度时缩略,否则原图重绘 if (ow > tw) { nh = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(ow))); } else { nw = ow; nh = oh; } break; case 5://高度大于缩略图高度时缩略,否则原图重绘 if (oh > th) { nw = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(oh))); } else { nw = ow; nh = oh; } break; case 6://原图重绘 nw = ow; nh = oh; break; case 7://宽度大于缩略图宽度时缩略,否则原图重绘 if ((ow > tw) || (oh > th)) { if (x1 > x2) { nh = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(ow))); } else { nw = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(oh))); } } else { nw = ow; nh = oh; } break; }//k值不在范围内时,强制缩略成原图,变形。 Bitmap bitmap = new System.Drawing.Bitmap(nw, nh, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 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(System.Drawing.Color.White); g.DrawImage(OImage, new System.Drawing.Rectangle(0, 0, nw, nh), new System.Drawing.Rectangle(a, b, c, d), System.Drawing.GraphicsUnit.Pixel);//绘图 g.Dispose(); return bitmap; } 服务器端读取图片,用Graphic.DrawImage按照比例缩放,绘制好了再存回去。 winform程序的小问题 notifyIcon1 控件ICO图标设置不了 TreeView.ExpandAll() 怎么让他停止。 字符和ACSII码 怎么获取DataGridView中显示的记录 急!c#如何连SQL Server数据库,谁能提供一个c#通过odbc连数据库的类啊? 装了VS.net2003,新建项目时弹出错误,Automation 服务器不能创建对象。帮忙解决 代码生成窗体和控件问题 请教自动登陆网页的winform程序,高分! win xp下如何用C#? WINFORM 定時上傳如何驗證文件已存在問題 一个C#小编程练习,求逆数对
public Bitmap MakeThumb(System.Drawing.Image OImage, int k, int tw, int th)
{
int ow = OImage.Width;
int oh = OImage.Height;
int a = 0;
int b = 0;
int c = ow;
int d = oh;
int nw = tw;
int nh = th;
double x1 = Convert.ToDouble(Convert.ToDouble(ow) / Convert.ToDouble(tw));
double x2 = Convert.ToDouble(Convert.ToDouble(oh) / Convert.ToDouble(th));
switch (k)
{
case 1://取中心部分
if (x1 > x2)
{
b = 0;
d = oh;
c = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(th)));
a = (ow - c) / 2;
}
else
{
a = 0;
c = ow;
d = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(tw)));
b = (oh - d) / 2;
}
break;
case 2://强制宽度符合缩略图
nh = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(ow)));
break;
case 3://强制高度符合缩略图
nw = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(oh)));
break;
case 4://宽度大于缩略图宽度时缩略,否则原图重绘
if (ow > tw)
{
nh = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(ow)));
}
else
{
nw = ow;
nh = oh;
}
break;
case 5://高度大于缩略图高度时缩略,否则原图重绘 if (oh > th)
{
nw = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(oh)));
}
else
{
nw = ow;
nh = oh;
}
break;
case 6://原图重绘
nw = ow;
nh = oh;
break;
case 7://宽度大于缩略图宽度时缩略,否则原图重绘 if ((ow > tw) || (oh > th))
{
if (x1 > x2)
{
nh = Convert.ToInt32(oh * Convert.ToDouble(Convert.ToDouble(tw) / Convert.ToDouble(ow)));
}
else
{
nw = Convert.ToInt32(ow * Convert.ToDouble(Convert.ToDouble(th) / Convert.ToDouble(oh)));
}
}
else
{
nw = ow;
nh = oh;
}
break;
}//k值不在范围内时,强制缩略成原图,变形。 Bitmap bitmap = new System.Drawing.Bitmap(nw, nh, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
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(System.Drawing.Color.White);
g.DrawImage(OImage, new System.Drawing.Rectangle(0, 0, nw, nh), new System.Drawing.Rectangle(a, b, c, d), System.Drawing.GraphicsUnit.Pixel);//绘图
g.Dispose();
return bitmap;
}