缩略图不变形

简介:

Public Shared Sub MakeSmallImg(ByVal postFile As System.Web.HttpPostedFile, ByVal saveImg As String, ByVal Width As System.Double, ByVal Height As System.Double)


        Dim originalFilename As String = postFile.FileName
        '生成的高质量图片名称
        Dim strGoodFile As String = saveImg


        '从文件取得图片对象
        Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(postFile.InputStream, True)

        Dim NewWidth, NewHeight As System.Double
        If (image.Width > image.Height) Then

            NewWidth = Width
            NewHeight = image.Height * (NewWidth / image.Width)

        Else

            NewHeight = Height
            NewWidth = (NewHeight / image.Height) * image.Width
        End If

        If (NewWidth > Width) Then

            NewWidth = Width
        End If
        If (NewHeight > Height) Then

            NewHeight = Height
        End If

        '取得图片大小
        Dim size As System.Drawing.Size = New System.Drawing.Size(CType(NewWidth, Integer), CType(NewHeight, Integer))
        '新建一个bmp图片
        Dim bitmap As System.Drawing.Image = New System.Drawing.Bitmap(size.Width, size.Height)
        '新建一个画板
        Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
        '设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
        '设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
        '清空一下画布
        g.Clear(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)

        g.Dispose()
        image.Dispose()
        bitmap.Dispose()

    End Sub 

MakeSmallImg(PhotoPath.PostedFile, filePath, 140, 185)

本文转自kenty博客园博客,原文链接http://www.cnblogs.com/kentyshang/archive/2006/11/21/566950.html如需转载请自行联系原作者


kenty

相关文章
|
4月前
|
前端开发 JavaScript
前端必看的8个HTML+CSS技巧 (六) 裁剪图像的动画
前端必看的8个HTML+CSS技巧 (六) 裁剪图像的动画
Photoshop - 怎么让画布大小自适应图像大小?
Photoshop - 怎么让画布大小自适应图像大小?
868 0
Photoshop - 怎么让画布大小自适应图像大小?
|
7月前
|
C++ 计算机视觉
C++-图像目标区裁剪ImageCropping
C++-图像目标区裁剪ImageCropping
|
算法 定位技术 C#
C#开发:不规则裁切图片
C#开发:不规则裁切图片
121 0
|
Java Maven
thumbmailator组件对图像的使用缩放、裁剪、旋转、格式钻换
thumbmailator组件对图像的使用缩放、裁剪、旋转、格式钻换
122 0
|
编解码
图片的变形与模糊,是两回事
图片的变形与模糊,是两回事
108 0
|
Windows
利用Adorner制作用于图像裁切的选择框
原文:利用Adorner制作用于图像裁切的选择框 前天,我写了一篇“使用Adorner显示WPF控件的边界点”的文章。这次,使用从Adorner继承来写一个用于图像裁切的选择框。
747 0
|
移动开发 计算机视觉 Python
把图片按照指定大小剪裁,不够的地方加黑边
1 # -*- coding: utf-8 -*- 2 3 import os 4 import sys 5 import numpy as np 6 import cv2 7 8 IMAGE_SIZE = 224 9 10 11 # 按照指定图像大小调整尺寸 12 de...
1215 0
方形图片转为圆形图片
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/79354214 在线工具:http://www.quickpicturetools.com/en/rounded_corners/ 打开 在线工具:http://www.quickpicturetools.com/en/rounded_corners/。
1559 0