asp.net – 调整白色图像后获得灰色边框
我正在搜索谷歌的某种解决方案,我发现了一个,我试图在我的代码中实现它,但它不起作用.问题是,在调整白色图像大小后,它们会变成灰色边框.
这是我找到的soloution的链接: 它说: https://groups.google.com/group/microsoft.public.dotnet.framework.drawing/browse_thread/thread/d834851b49274fd9/81a4fd43694457ac?hl=en&lnk=st&q=DrawImage+resized+border#81a4fd43694457ac 代码1:这是我的代码实现ImageAttributes: Private Shared Function ResizeImageFile(ByVal imageFile As Byte(),ByVal targetSize As Integer) As Byte() Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile)) Dim newSize As Size = CalculateDimensions(oldImage.Size,targetSize) Using newImage As New Bitmap(newSize.Width,newSize.Height,PixelFormat.Format32bppRgb) Using canvas As Graphics = Graphics.FromImage(newImage) Using ia As New ImageAttributes ia.SetWrapMode(Drawing2D.WrapMode.TileFlipXY) canvas.SmoothingMode = SmoothingMode.AntiAlias canvas.InterpolationMode = InterpolationMode.HighQualityBicubic canvas.PixelOffsetMode = PixelOffsetMode.HighQuality canvas.DrawImage(oldImage,New Rectangle(New Point(0,0),newSize),newImage.Width,newImage.Height,GraphicsUnit.Pixel,ia) Dim m As New MemoryStream() newImage.Save(m,ImageFormat.Png) Return m.GetBuffer() End Using End Using End Using End Using End Function Private Shared Function CalculateDimensions(ByVal oldSize As Size,ByVal targetSize As Integer) As Size Dim newSize As New Size() If oldSize.Height > oldSize.Width Then newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height)))) newSize.Height = targetSize Else newSize.Width = targetSize newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width)))) End If Return newSize End Function 代码2:在白色图像上涂抹灰色边框的代码 这是调整大小后的图像: 新图像尺寸宽度= 400px Private Shared Function ResizeImageFile(ByVal imageFile As Byte(),PixelFormat.Format32bppRgb) Using canvas As Graphics = Graphics.FromImage(newImage) canvas.SmoothingMode = SmoothingMode.AntiAlias canvas.InterpolationMode = InterpolationMode.HighQualityBicubic canvas.PixelOffsetMode = PixelOffsetMode.HighQuality canvas.DrawImage(oldImage,newSize)) Dim m As New MemoryStream() newImage.Save(m,ImageFormat.Png) Return m.GetBuffer() End Using End Using End Using End Function Private Shared Function CalculateDimensions(ByVal oldSize As Size,ByVal targetSize As Integer) As Size Dim newSize As New Size() If oldSize.Height > oldSize.Width Then newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height)))) newSize.Height = targetSize Else newSize.Width = targetSize newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width)))) End If Return newSize End Function 更新30.07.2011: CODE 1解决了白色图像上灰色边框的问题,但是存在新问题.问题出在这行代码中: canvas.DrawImage(oldImage,ia) 使用此代码,我得到具有所需宽度和高度的输出图像,没有灰色边框,但oldImage没有缩放. 例如: 如果我想上传,调整大小并保存有形的图像,例如640x480px,而targetSize是400px.作为输出,我得到一个宽度为400px,高度:300px的图像,但oldImage没有调整大小/缩放到400px.在这种情况下,oldImage以原始分辨率绘制.如何缩放oldImage以正确绘制?有人能指出我正确的解决方案或修改代码吗? 对每个人而言,但我找到了解决所有问题的方法. 由于以下代码行,CODE 1无法正常工作: canvas.DrawImage(oldImage,ia) 解: canvas.DrawImage(oldImage,oldImage.Width,oldImage.Height,ia) 这是完整的工作代码(没有灰色/黑色边框的调整大小的图像): Private Shared Function ResizeImageFile(ByVal imageFile As Byte(),ByVal targetSize As Integer) As Size Dim newSize As New Size() If oldSize.Height > oldSize.Width Then newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height)))) newSize.Height = targetSize Else newSize.Width = targetSize newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width)))) End If Return newSize End Function 解决方法
这是我所拥有的类中的一个函数,您将不得不替换一些类属性(ThumbNailSize.Width,ThumbNailSize.Height):
public void ResizeImage(HttpPostedFile fil,string sPhysicalPath,string sOrgFileName,string sThumbNailFileName,ImageFormat oFormat,int rez) { try { System.Drawing.Image oImg = System.Drawing.Image.FromStream(fil.InputStream); decimal pixtosubstract = 0; decimal percentage; //default Size ThumbNailSizeToUse = new Size(); if (ThumbNailSize.Width < oImg.Size.Width || ThumbNailSize.Height < oImg.Size.Height) { if (oImg.Size.Width > oImg.Size.Height) { percentage = (((decimal)oImg.Size.Width - (decimal)ThumbNailSize.Width) / (decimal)oImg.Size.Width); pixtosubstract = percentage * oImg.Size.Height; ThumbNailSizeToUse.Width = ThumbNailSize.Width; ThumbNailSizeToUse.Height = oImg.Size.Height - (int)pixtosubstract; } else { percentage = (((decimal)oImg.Size.Height - (decimal)ThumbNailSize.Height) / (decimal)oImg.Size.Height); pixtosubstract = percentage * (decimal)oImg.Size.Width; ThumbNailSizeToUse.Height = ThumbNailSize.Height; ThumbNailSizeToUse.Width = oImg.Size.Width - (int)pixtosubstract; } } else { ThumbNailSizeToUse.Width = oImg.Size.Width; ThumbNailSizeToUse.Height = oImg.Size.Height; } Bitmap bmp = new Bitmap(ThumbNailSizeToUse.Width,ThumbNailSizeToUse.Height); bmp.SetResolution(rez,rez); System.Drawing.Image oThumbNail = bmp; bmp = null; Graphics oGraphic = Graphics.FromImage(oThumbNail); oGraphic.CompositingQuality = CompositingQuality.HighQuality; oGraphic.SmoothingMode = SmoothingMode.HighQuality; oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic; Rectangle oRectangle = new Rectangle(0,ThumbNailSizeToUse.Width,ThumbNailSizeToUse.Height); oGraphic.DrawImage(oImg,oRectangle); oThumbNail.Save(sPhysicalPath + sThumbNailFileName,oFormat); oImg.Dispose(); } catch (Exception ex) { Response.Write(ex.Message); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 我如何使用AJAX来确定用户的会话是否已过期,然后
- asp.net-mvc – Asp.net MVC 1和Asp.net MVC 2之间的区别
- asp.net – 在Global.asax中使用Trace
- asp.net – 命名空间’System’中不存在类型或命名空间’Li
- asp.net中的TDD新手,我是否在正确的轨道上编写测试?
- asp.net – 重启后,每个页面的MVC应用程序都很慢
- asp.net – Simplemembership – 添加电子邮件字段并用作登
- asp.net-mvc – ASP.NET MVC缓存如何为AJAX请求工作?
- 初识ABP vNext(10):ABP设置管理
- asp.net-mvc – 在Razor View中重命名mvc模型对象
- asp.net-mvc-3 – RavenDb,无法访问文件,文件被锁
- asp.net-mvc-3 – MVC3下拉列表未选择所选项目
- asp.net – 从User.Identity.Name中删除域名
- asp.net-mvc – ViewBag在RedirectToAction之后为
- asp.net-mvc – 在哪里可以找到与asp.net MVC一起
- ASP.NET MVC 4 – Ajax.BeginForm和html5
- asp.net-mvc – ASP.NET MVC RenderAction重新呈
- asp.net-mvc-5 – 我需要在MVC Razor中编码属性值
- 像ASP.NET WebControls一样,在dll中可重用的ASP.
- asp.net核心 – ASP.NET 5,DNX&Kestrel:没有