110,566
社区成员
发帖
与我相关
我的任务
分享
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;
}