在vb.net中调整图像大小 [英] resize image in vb.net

查看:601
本文介绍了在vb.net中调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在此代码中添加什么来调整我保存的宽度为100px的图像的大小



what should i add to this code to resize the images that i am saving to be of width 100px

Dim extension1 As String = Path.GetExtension(Fileupload1.PostedFile.FileName)
           If extension1.ToString = ".png" Or extension1.ToString = ".jpg" Or extension1.ToString = ".bmp" Or extension1.ToString = ".gif" Then
               Dim strdir1 As String = Server.MapPath("uploads/pics/")
               Fileupload1.PostedFile.SaveAs(strdir1 & cmd_id & extension1)
               textbox2.Text = cmd_id & extension1









谢谢





Thanks

推荐答案

这称为重新取样



使用<$ c $类读取图像c> System.Drawing.Bitmap 并创建一个所需大小的全新图像:

http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/7we6s1x3.aspx [ ^ ]。



对于这张新图片,获取 System.Drawing.Graphics 的实例;它可用于绘制图像:

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx [ ^ ]。



绘制原始图像第二个,使用适当的大小调整,使用上一步获得的 Graphic 实例。首先,选择重新采样的插值质量:

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode.aspx [ ^ ]。



并且抽奖: http://msdn.microsoft.com/en-us/library/yws82c40.aspx [ ^ ]。



您应该记住,重新采样位图只有在缩小尺寸时才会有用,在最坏的情况下,放大,但只是略微缩小。任何相当大的放大都会导致质量差,像素化。



-SA
This is called resampling.

Read the image using the class System.Drawing.Bitmap and create a brand new image of required size:
http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx[^],
http://msdn.microsoft.com/en-us/library/7we6s1x3.aspx[^].

For this new image, obtain the instance of System.Drawing.Graphics; it can be used to draw on the image:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[^].

Draw original image on the second one, with appropriate resize, using the Graphic instance obtained on the previous step. First of all, choose the quality of interpolation for resampling:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode.aspx[^].

And draw: http://msdn.microsoft.com/en-us/library/yws82c40.aspx[^].

You should remember that resampling bitmaps can only be good if you reduce the size, in worst case, enlarge, but only slightly. Any considerable enlargement will give bad quality, pixellation.

—SA


首先,停止呼叫ToString已经是String的项目 - 它不需要,它浪费时间和资源,看起来你不知道你在做什么! :笑:



为了调整图像大小,您必须先将其作为图像加载,然后将其重新绘制为不同的大小。假设您已经知道如何将数据转换为某种形式的图像,Image类有一个方法Image.GetThumbnailImage,它可以为您完成所有工作 - 它按比例调整图像大小到您想要的矩形,保持纵横比: a href =http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 > MSDN [ ^ ]

该链接包含一个示例。
Firstly, stop calling ToString on items which are already String - it isn't needed, it wastes time and resources, and it looks like you don't know what you are doing! :laugh:

In order to resize an image, you have to first load it as an image, and then re-draw it to a different size. Assuming you already know how to convert your data to an image of some form, the Image class has a method Image.GetThumbnailImage which does all the work for you - it proportionally resizes the image into the rectangle you want, maintaining the aspect ratio: MSDN[^]
The link includes an example.


看看我对这个类似问题的回答:



图形,VB.NET中的图像处理



我提供了一个可以用来做你需要的课程。



希望这有帮助,



- Pete
Check out my answer to this similar question:

Graphics, Image Manipulation in VB.NET

I provide a class you can use to do what you need.

Hope this helps,

- Pete


这篇关于在vb.net中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆