.net自动缩放图片 高手请进

yjcel1988 2010-05-25 03:37:55
.net如何实现自动缩放图片,而不失真。请高手指点。
...全文
1533 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2010-05-25
  • 打赏
  • 举报
回复
4个方法 希望能对你有帮助。
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图宽度</param>
/// <param name="mode">缩略图高度</param>
/// <param name="type">生成缩略图的方式 </param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string type)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int towidth = width;
int toheight = height;

int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;

switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放)
if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight)
{
toheight = height;
towidth = originalImage.Width * height / originalImage.Height;
}
else
{
towidth = width;
toheight = originalImage.Height * width / originalImage.Width;
}
break;
default:
break;
}

//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);

try
{
//保存缩略图
if (type == "JPG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
if (type == "BMP")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
}
if (type == "GIF")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
}
if (type == "PNG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}

/**/
///
/// 在图片上增加文字水印
///
/// 原服务器图片路径
/// 生成的带文字水印的图片路径
protected void AddShuiYinWord(string Path, string Path_sy)
{
string addText = "测试水印";
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);

g.DrawString(addText, f, b, 15, 15);
g.Dispose();

image.Save(Path_sy);
image.Dispose();
}

/**/
///
/// 在图片上生成图片水印
///
/// 原服务器图片路径
/// 生成的带图片水印的图片路径
/// 水印图片路径
protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose();

image.Save(Path_syp);
image.Dispose();
}


/// <summary>
/// 删除文件文件或图片
/// </summary>
/// <param name="path">当前文件的路径</param>
/// <returns>是否删除成功</returns>
public static bool FilePicDelete(string path)
{
bool ret = false;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
file.Delete();
ret = true;
}
return ret;
}
z050301402 2010-05-25
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
/// <summary>
///工具类
/// </summary>
public class Common
{


#region 生成缩略图
//生成缩略图的二个涵数-------------------------------------------
public static Size NewSize(int maxWidth, int maxHeight, int width, int height)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(width);
double sh = Convert.ToDouble(height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);

if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}

return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="path">存放原始图片的文件夹路径</param>
/// <param name="fileName">原始图片名</param>
/// <param name="maxWidth">最大宽度</param>
/// <param name="maxHeight">最大高度</param>
/// <param name="Prefix">缩略图的前缀,可以随意定义,例如:如果Prefix参数为 "sl" ,当原始图片名为" 43.jpg ",那么生成
/// 的缩略图文件名就为 " sl43.jpg"
/// </param>
public static void SendSmallImage(string path, string fileName, int maxWidth, int maxHeight,string Prefix)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(path + fileName);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;

Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);

// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();

if (thisFormat.Equals(ImageFormat.Gif))
{
HttpContext.Current.Response.ContentType = "image/gif";
}
else
{
HttpContext.Current.Response.ContentType = "image/jpeg";
}

// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;

//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置JPEG编码
break;
}
}

if (jpegICI != null)
{
outBmp.Save(path + Prefix + fileName, jpegICI, encoderParams);
//outBmp.Save(Response.OutputStream, jpegICI, encoderParams);
}
else
{
outBmp.Save(path + Prefix + fileName, thisFormat);
}

img.Dispose();
outBmp.Dispose();
HttpContext.Current.Response.ContentType = "text/html";
}
//缩略图代码结束----------------------------------------------------
#endregion


}
这样就不会失真了 qq:50301402
wuyq11 2010-05-25
  • 打赏
  • 举报
回复
等比例缩放图片
<script language="JavaScript">
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}

}
}onload="javascript:DrawImage1(this)">
上传图片生成缩略图
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW":
break;
case "W":
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H":
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut":
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent);
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);

try
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}


kwp_911229 2010-05-25
  • 打赏
  • 举报
回复


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>上传图片前预览 </TITLE>
<script>
//此方法只能用在HTML页面中
function test(){
document.getElementById("showimg").src=document.getElementById("file1").value;


}

/*
google一下能用Microsoft.AlphaImageLoader滤镜解决此问题.此滤镜的主要作用就是对

图片进行透明处理。虽然FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,但是就

IE5-IE6而言还是有一定的意义。

语法:
filter : progid:DXImageTransform.Microsoft.AlphaImageLoader (enabled=bEnabled

, sizingMethod=sSize , src=sURL )

属性:
1 enabled:true/false 是否激活滤镜
2 sizingMethod:
01 crop:剪切图片以适应对象尺寸。
02 image:默认值。增大或减小对象的尺寸边界以适应图片的尺寸。
03 scale:缩放图片以适应对象的尺寸边界
3 src属性是指向图片绝对或相对路径的(必选的);

此方法问题:浏览图片是没问题,可是浏览小图Div会小,浏览大图可能会占据整个页面,

而我们希望以固定的大小显示所有的图片
*/
function ShowImage1(path){
document.getElementById("divShow").filters.item

("DXImageTransform.Microsoft.AlphaImageLoader").src = path;
}

/*
为了解决上面的问题,我们可以使用他的scale属性.可是如果用这个属性,在浏览大图时

,他会缩小原图;浏览小图时,他会放大小图,使小图模糊不清。所以不能用此属性。最

后我们添加下面的setImg(o)方法改变这种情况。
*/
function ShowImage(path){
//处理前是原图,先将其隐藏,
document.all.divShow.style.visibility="hidden";
document.all.divShow.filters.item

("DXImageTransform.Microsoft.AlphaImageLoader").src=path;
document.all.divShow.filters.item

("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod="image";

//过100毫秒后获取div的宽高.
setTimeout("setImg(document.all.divShow)",100);
//setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。
document.all.divShow.style.border="1px solid #ccc";
}

function setImg(o){
var img_width;
var img_height;

o.style.visibility = "visible";
img_width=o.offsetWidth; //o.offsetWidth对象的可见宽度
img_height=o.offsetHeight; //o.offsetWidth对象的可见高度

var width=274; //预定义宽
var height=100; //预定义高

var ratW; //宽的缩小比例
var ratH; //高的缩小比例
var rat; //实际使用的缩小比例

if(img_width<width && img_height<height)
{
//如果比预定义的宽高都小,原图显示。
o.filters.item

("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod="image";
return;
}else{
//如果大的话,要把 sizingMethod改成scale; 如果属性是image,不管怎么改div的

宽高,都不起作用
o.filters.item

("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod="scale";
}

ratW=width/img_width;
ratH=height/img_height;

if(ratH<ratW)//选择最小的作为实际的缩小比例
rat=ratH;
else
rat=ratW;

img_width=img_width * rat;
img_height=img_height * rat;

o.style.width=img_width;
o.style.height=img_height;
}
</script>
</HEAD>

<BODY MS_POSITIONING="GridLayout">

<!--
<img id="showimg">
<input type="file" id="file1" onchange="test()">
-->
<form id="Form1" method="post">
<input type="file" onchange="ShowImage(this.value)" >

<div id="divShow"

style="width:274px;height:100px;FILTER:progid:DXImageTransform.Microsoft.Alpha

ImageLoader(sizingMethod=image);"></div>
</form>

</BODY>
</HTML>




里面的ShowImage(this.value) 是调用事件!
hxbr110 2010-05-25
  • 打赏
  • 举报
回复
学习了。。。。。。。。。。。。。。。。。。。。
理不完的逻辑 2010-05-25
  • 打赏
  • 举报
回复
帮顶 学习~!
PaulLeder 2010-05-25
  • 打赏
  • 举报
回复
.NET本身就有缩略图的啊
/// <summary>
/// 回调
/// </summary>
/// <returns></returns>
public bool ThumbnailCallback()
{
return false;
}
/// <summary>
/// 生成缩略图,返回缩略图的Image对象
/// </summary>
/// <param name="Width">缩略图宽度</param>
/// <param name="Height">缩略图高度</param>
/// <returns>缩略图的Image对象</returns>
public Image GetImage(int Width, int Height)
{
Image img;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
img = srcImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
return img;
}
T_long 2010-05-25
  • 打赏
  • 举报
回复
ke5315309 2010-05-25
  • 打赏
  • 举报
回复
你按比例来就不会..不然就会..
dongqi0517 2010-05-25
  • 打赏
  • 举报
回复
高手来解答~给你个我自己写的万能类,【图面无损】
 /// <summary>
///
/// </summary>
/// <param name="fileurl">原图URL</param>
/// <param name="minwidth">缩略图宽</param>
/// <param name="minheight">缩略图高</param>
/// <param name="FileName">上传到“”文件夹下目录(文件夹名)</param>
/// <param name="filename">原图文件名</param>
/// <returns></returns>
public static void SendToSmallImage(string fileurl, int minwidth, int minheight, string FileName,string filename)
{
//public static string SendToSmallImage(Stream _stream, int minwidth, int minheight, string FileName, bool type)
//System.Drawing.Image img = System.Drawing.Image.FromStream(_stream);

System.Drawing.Image img = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(fileurl));

int lenEnd = fileurl.LastIndexOf('.');
string fileType = fileurl.Substring(lenEnd, fileurl.Length - lenEnd);


ImageFormat imgformat = img.RawFormat;

Size newSize = NewSize(minwidth, minheight, img.Width, img.Height);

Bitmap bm = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(bm);

// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();

// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;

//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
ImageCodecInfo GIFICI = null;
ImageCodecInfo jpgICI = null;
ImageCodecInfo pngICI = null;
ImageCodecInfo bmpICI = null;

ImageCodecInfo allICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
jpegICI = arrayICI[x];//设置JPEG编码

if (arrayICI[x].FormatDescription.Equals("GIF"))
GIFICI = arrayICI[x];//设置GIF编码

if (arrayICI[x].FormatDescription.Equals("JPG"))
jpgICI = arrayICI[x];//设置GIF编码

if (arrayICI[x].FormatDescription.Equals("PNG"))
pngICI = arrayICI[x];//设置GIF编码

if (arrayICI[x].FormatDescription.Equals("BMP"))
bmpICI = arrayICI[x];//设置GIF编码

}

string stringpath = null;
string imagename = filename;
if (fileType == ".jpg")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = jpegICI;

}
else if (fileType == ".gif")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = GIFICI;

}
else if (fileType == ".jpg")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = jpgICI;

}
else if (fileType == ".png")
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = pngICI;

}
else
{
stringpath = "/imageUpload/" + FileName + "/" + imagename;
allICI = bmpICI;

}

try
{
bm.Save(HttpContext.Current.Server.MapPath(stringpath), allICI, encoderParams);
}
catch (Exception e)
{
throw e;
}
finally
{
img.Dispose();
bm.Dispose();
}

//return imagename;
}


/// <summary>
///
/// </summary>
/// <param name="minwidth">微缩图要设置的宽度</param>
/// <param name="minheight">微缩图要设置的长度</param>
/// <param name="forwidth">原图宽度</param>
/// <param name="forheight">原图长度</param>
/// <returns></returns>
private static Size NewSize(int minwidth, int minheight, int forwidth, int forheight)
{
int newwidth;
int newheight;
if (forwidth > forheight)
{
newwidth = minwidth;
newheight = newwidth * forheight / forwidth;
}
else
{
newheight = minheight;
newwidth = newheight * forwidth / forheight;
}
System.Drawing.Size _Size = new Size();
_Size.Height = newheight;
_Size.Width = newwidth;
return _Size;
}
gdlpc 2010-05-25
  • 打赏
  • 举报
回复
缩小的(如缩略图)是不会失真的
yjcel1988 2010-05-25
  • 打赏
  • 举报
回复
高手,进来呀
yjcel1988 2010-05-25
  • 打赏
  • 举报
回复
是这样的,我是想上传一张图片后,再生成一张小图片
T_long 2010-05-25
  • 打赏
  • 举报
回复
我只做过一个
嫌图片传上去太大,可以自己制定宽度和高度,不知道楼主是什么意思?
马老虎 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mmm306306 的回复:]

引用 2 楼 mmm306306 的回复:

没有自动的。
都是通过计算等比例缩放的


C# code

/// <summary>
/// 产生原图的缩略图
/// </summary>
/// <param name="souceImage">原图 Image 对象</param>
/// <param n……
[/Quote]


//用于控制图片缩略图显示
function zoom(image, width, height)
{
var scale = image.width/image.height;
if (image.width > width || image.height > height)
{
// 按比例转换
image.width = scale * height < width ? (height * scale) : width;
image.height = scale * height >= width ? (width / scale) : height;
}
}

yjcel1988 2010-05-25
  • 打赏
  • 举报
回复
.net如何实现上传时自动缩放图片,而不失真。请高手指点
gdlpc 2010-05-25
  • 打赏
  • 举报
回复
如果是放大的话应该是有失真的吧(我是用过图形处理软件试过,在网页上未试过)
马老虎 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mmm306306 的回复:]

没有自动的。
都是通过计算等比例缩放的
[/Quote]


/// <summary>
/// 产生原图的缩略图
/// </summary>
/// <param name="souceImage">原图 Image 对象</param>
/// <param name="ImageUrl">原图路径</param>
/// <param name="wight">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static void ZoomImage(Image souceImage, string ImageUrl, int wight, int height,int languageType)
{
//获得图片的绝对路径
string NewImageUrl = System.Web.HttpContext.Current.Server.MapPath(@ImageUrl);
//赋Image对象 路径
souceImage.ImageUrl = ImageUrl;

if (File.Exists(NewImageUrl))//该图片是存在
{
//将指定图片转成数据流,建Drawing.Image对象
FileStream Pic = new FileStream(NewImageUrl, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] PicByte = new byte[Pic.Length];
Pic.Read(PicByte, 0, PicByte.Length);
System.Drawing.Image img = System.Drawing.Image.FromStream(Pic);

double scale = (float)img.Width / img.Height;
if (img.Width > wight || img.Height > height)
{
souceImage.Width = Unit.Pixel(Convert.ToInt32(Math.Round((scale * height < wight ? (scale * height) : wight), 0)));
souceImage.Height = Unit.Pixel(Convert.ToInt32(Math.Round((scale * height >= wight ? (wight/scale) : height), 0)));
}
Pic.Dispose();
img.Dispose();
}
else//该图片不存在
{
if (1 == languageType)
{
souceImage.ImageUrl = "~/images/noneImageEn.jpg";//给Image路径赋个无图片的图片
}
else
{
souceImage.ImageUrl = "~/images/noneImage.jpg";//给Image路径赋个无图片的图片
}
souceImage.Width = Unit.Pixel(wight);
souceImage.Height = Unit.Pixel(height);
}

}

aiguo0713 2010-05-25
  • 打赏
  • 举报
回复
是想要在鼠标滚轮事件里写吗?

图片是否失真应该只是和图片格式有关吧?
马老虎 2010-05-25
  • 打赏
  • 举报
回复
没有自动的。
都是通过计算等比例缩放的
加载更多回复(1)

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧