如题,一个像素的纯色图片放大50倍,颜色从左上角开始逐渐变浅,代码如下: Bitmap bmp = Bitmap.FromFile("D:\test.png") as Bitmap; Bitmap newBitmap = new Bitmap(50, 50); Graphics g = Graphics.FromImage(newBitmap); g.DrawImage(bmp, new Rectangle(0, 0, 50, 50), new Rectangle(0, 0, 1, 1), GraphicsUnit.Pixel); newBitmap.Save("D:\newTest.png"); g.Dispose(); newBitmap.Dispose(); mp.Dispose(); |
|
我期望是1*1的黑色图片,放大为一个50*50像素的黑色图片,但是现在出现的结果是从左上角开始逐渐变淡的图片,如果我用2*2的黑色的图片,就没有这个问题,这是什么原因
|
|
5分 |
如何生成高清略缩图不失真
//上传图片 protected void imgBtnLoad_Click(object sender, ImageClickEventArgs e) { if (!fulPhoto.HasFile) { lbMessage.Text = “请选择上传图片!”; return; } else { try { //获取上传文件路径 string filePath = fulPhoto.PostedFile.FileName; //获取上传文件后缀 string fileExt = filePath.Substring(filePath.LastIndexOf(“.”) + 1); //限定上传格式 if (fileExt.ToLower() == “gif” || fileExt.ToLower() == “jpg” || fileExt.ToLower() == “bmp” || fileExt.ToLower() == “png”) { if (fulPhoto.PostedFile.ContentLength > 5120000) { lbMessage.Text = “限定上传图片的大小不能超出5M!”; return; } else { //根据时间生成文件名 string nowTime = Album.CreateDateTimeString(); string fileName = nowTime + “.” + fileExt; //源文件保存路径 string savePath = Server.MapPath(“UpFile/”); //缩略图保存路径 string imgPath = Server.MapPath(“UpSmall/”); //上传图片 fulPhoto.PostedFile.SaveAs(savePath + fileName); //创建自定义Album类对象实例 Album am = new Album(); //根据图片的宽、高比例生成缩略图 System.Drawing.Image img = System.Drawing.Image.FromFile(savePath + fileName); if (img.Width >= img.Height) { am.GetThumbnail(savePath + fileName, imgPath + fileName, 400, 300, “Cut”); } else { am.GetThumbnail(savePath + fileName, imgPath + fileName, 320, 350, “Cut”); } //文件类型 string p_type = fulPhoto.PostedFile.ContentType; //文件大小 int p_size = fulPhoto.PostedFile.ContentLength; int categoryId = Convert.ToInt32(ddlCategory.SelectedValue); //调用类方法将数据插入到数据库 int result = am.AddPhoto(tbName.Text.Trim(), tbDescript.Text.Trim(), fileName, p_type, p_size, categoryId); ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), “scriptname”, “alert(“图片上传成功!”);”, true); } } else { lbMessage.Text = “只允许上传gif,jpg,bmp,png格式的图片文件!”; return; } } catch (Exception ex) { throw new Exception(ex.Message, ex); } } }//codego.net/tags/11/1/ //调用自定义方法生成略缩图 public void GetThumbnail(string serverImagePath, string thumbnailImagePath, int width, int height, string p) { System.Drawing.Image serverImage = System.Drawing.Image.FromFile(serverImagePath); //画板大小 int towidth = width; int toheight = height; //缩略图矩形框的像素点 int x = 0; int y = 0; int ow = serverImage.Width; int oh = serverImage.Height; switch (p) { case “WH”://指定高宽缩放(可能变形) break; case “W”://指定宽,高按比例 toheight = serverImage.Height * width / serverImage.Width; break; case “H”://指定高,宽按比例 towidth = serverImage.Width * height / serverImage.Height; break; case “Cut”://指定高宽裁减(不变形) if ((double)serverImage.Width / (double)serverImage.Height > (double)towidth / (double)toheight) { oh = serverImage.Height; ow = serverImage.Height * towidth / toheight; y = 0; x = (serverImage.Width – ow) / 2; } else { ow = serverImage.Width; oh = serverImage.Width * height / towidth; x = 0; y = (serverImage.Height – oh) / 2; } break; default: break; } //新建一个bmp图片 System.Drawing.Image bm = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(serverImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bm.Save(thumbnailImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { serverImage.Dispose(); bm.Dispose(); g.Dispose(); } } /div> |
5分 |
与其纠结于为啥放大N倍就有失真问题,为啥不考虑改变策略,提供不同比例的图片,按需取对应图片,因为图片失真这是没办法的事情,你还需要了解下图片失真跟图片保存方式之间的关系
|
现在是用两个像素的图片放大替代了,放大两个像素的图片没有问题,只是想了解一下这个原因。
|
|
10分 |
base.OnPaint(e); Bitmap bmp = new Bitmap(1,1); using(Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.Red); //g.FillEllipse(Brushes.Black, new Rectangle(0, 0, 10, 10)); } System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes(); attr.SetWrapMode(WrapMode.Tile); //这个是关键 e.Graphics.DrawImage(bmp,new Rectangle(0,0,50,100), 0,0,bmp.Width,bmp.Height,GraphicsUnit.Pixel, attr,null,IntPtr.Zero); /pre> |
你想”放大”后不失真,应该自己获取它的颜色然后填充进新的图像里,而不是直接调用系统函数让它自己”放大”
|