主要利用://在指定位置畫圖
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);
image:你的原圖,
System.Drawing.Rectangle:位置與長寬,根據(jù)自己需要設置!
--------------------------------------------------------------------------------------------------------------
實例如下:
public void MakeSmallImg(string filePath, string saveImg)
{
//從文件取得圖片對象
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath, true);
//取得圖片大小
System.Drawing.Size size = new System.Drawing.Size((int)image.Width, (int)image.Height);
//新建一個bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質(zhì)量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
//設置高質(zhì)量,低速度呈現(xiàn)平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
//清空一下畫布
g.Clear(System.Drawing.Color.White);
//在指定位置畫圖
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.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋體",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();
///圖片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存高清晰度的縮略圖
// bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
// 加個a表示是縮略圖
bitmap.Save(saveImg, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
bitmap.Dispose();
}
=====================================================
c#縮小圖片后不清晰,要怎么縮小才能跟原來一樣清晰。我用:System.Drawing.Bitmap縮小的。
正好之前有寫過這個,不過是用的.net自帶的api寫的,如果想要弄的很好的話非常難,有興趣可以看一下插值法,有若干種辦法不一一列舉。
/// <summary>
/// 獲取縮小后的圖片
/// </summary>
/// <param name="bm">要縮小的圖片</param>
/// <param name="times">要縮小的倍數(shù)</param>
/// <returns></returns>
private Bitmap GetSmall(Bitmap bm, double times)
{
int nowWidth = (int)(bm.Width / times);
int nowHeight = (int)(bm.Height / times);
Bitmap newbm = new Bitmap(nowWidth, nowHeight);//新建一個放大后大小的圖片
if (times >= 1 && times <= 1.1)
{
newbm = bm;
}
else
{
Graphics g = Graphics.FromImage(newbm);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(bm, new Rectangle(0, 0, nowWidth, nowHeight), new Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel);
g.Dispose();
}
return newbm;
}
|