如何将一张小图片变成大图片
或者将大图片缩小 我本想实现js放大镜特效需要一张大图和一张小图
由于后台只能上传一张图片所以我想通过程序将图片变大或者缩小 希望大家给点源码谢谢大家了!
或者将大图片缩小 我本想实现js放大镜特效需要一张大图和一张小图
由于后台只能上传一张图片所以我想通过程序将图片变大或者缩小 希望大家给点源码谢谢大家了!
解决方案 »
- 问一个关于Dataset的问题。
- 急..........求救.... .net放在虚拟空间的子目录下有问题....
- 该去面试还是继续学习,并散分
- 怎么添加odbc数据源作为 ado .net entity data model
- 【急】未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序。
- 各位喜欢美女的朋友请进来 .很多美女哦^_^
- 求JS的时间控件
- 请问怎样刷新本页面?很简单的问题
- 我装的xp+sp2 运行asp.net 装sql 2000的什么版好啊?在线等,急!!!
- 多条记录提交到数据库
- 如何做 某个页面在线会员信息,求代码,马上给分
- 网站不间断出来如此情况,重启服务器又好了,真是郁闷啊呀
Byte[] FileByteArray = new Byte[UpFile.ContentLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
StreamObject.Read(FileByteArray, 0, UpFile.ContentLength);
System.Drawing.Image MyImage = System.Drawing.Image.FromStream(StreamObject);
int width = MyImage.Width;
int height = MyImage.Height;
double rate = (width * 1.00) / (height * 1.00);
if (width > 400 || height > 300)
{
if (rate > 1.33)
{
if (width > 400)
{
width = 400;
}
height = (int)(width / rate);
}
else
if (rate < 1.33)
{
rate = (height * 1.00) / (width * 1.00); if (height > 300)
{
height = 300;
}
width = (int)(height / rate);
}
else
{
width = 400;
height = 300;
} MemoryStream pic = new MemoryStream();
pic = ResizeImageFile(StreamObject, width, height);
FileByteArray = pic.ToArray();
}
else
{
StreamObject.Read(FileByteArray, 0, UpFile.ContentLength);
}记得结贴。
#region 生成缩略图的代码
private static MemoryStream ResizeImageFile(Stream stream, int width, int height)
{
Size newSize = new Size(width, height);
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(stream))
{
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
MemoryStream m = new MemoryStream();
newImage.Save(m, ImageFormat.Jpeg);
return m;
}
}
}
}
#endregion
VB.NET 上传时将图片更改为缩略图
Dim sfile As String = "file/mytx.gif"
Dim fileNames As String
If Me.Filename.PostedFile.ContentLength > 0 Then
fileNames = System.IO.Path.GetFileName(Request.Files.Item(0).FileName)
sfile = "/" & Date.Now.ToString("yyMMddhhmmss") & Request.UserHostAddress.ToString & fileNames
Try
GetThumbNail(Filename.PostedFile.FileName, 100, 120, Filename.PostedFile.ContentType.ToString(), False, Filename.PostedFile.InputStream, sfile)
' Me.Filename.PostedFile.SaveAs(Server.MapPath("File") & sfile)
Me.LabSc.Text = "上传文件" & fileNames & "成功"
Catch ex As Exception
Me.LabSc.Text = "上传文件" & fileNames & "失败" & ex.ToString
Return
End Try
sfile = "file" & sfile
End If---------------------------------------------------------------------------------------------------------------------------Private Sub GetThumbNail(ByVal strFileName, ByVal iWidth, ByVal iheight, ByVal strContentType, _
ByVal blnGetFromFile, ByVal ImgStream, ByVal filename)
Dim sfile As String = "file/mytx.gif"
Dim oImg As Image
If blnGetFromFile Then
oImg = oImg.FromFile(strFileName)
Else
oImg = oImg.FromStream(ImgStream)
End If
oImg = oImg.GetThumbnailImage(iWidth, iheight, Nothing, (New IntPtr).Zero) Dim str As String = filename.ToString
sfile = Date.Now.ToString("yyMMddhhmmss") & Request.UserHostAddress.ToString & strFileName
Dim strGuid As String = str.Substring(0, str.LastIndexOf("."))
strGuid = strGuid
strGuid = strGuid & str.Substring(str.LastIndexOf("."))
'保存到本地()
oImg.Save(Server.MapPath("File") + "\" + strGuid)
End Sub
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
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;
} //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel); long[] quality = new long[1];
quality[0] = 100; System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo jpegICI = null;
for (int i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[i];//设置JPEG编码
break;
}
} try
{
//以jpg格式保存缩略图
//bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Save(thumbnailPath, jpegICI, encoderParams);
}
catch (System.Exception e)
{ }
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}