请问如何在把图片保存到数据库时,让图片变小 请问如何在把图片保存到数据库时让图片变小就跟ps改变图像大小那样,尺寸而不改变图片本身现在一张图片一般都3mb左右 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 C#将图片像素变低,就可以变小了http://topic.csdn.net/u/20100506/23/006f8270-4991-4279-ab20-0c274a76cb98.html?13579 像素变低就改变图片的质量了,可以先把图片读成byte[],然后可以使用 GZip 等压缩方法进行压缩,可以有效压缩;const int UNZIP_BUFFER_LENGTH = 1024 * 1024; /// <summary>压缩数据流</summary> /// <param name="fileByteArray">需要压缩的数据流</param> /// <returns>返回压缩后的数据流</returns> public static byte[] ZipFileStream(byte[] fileBytes) { if (fileBytes == null) return null; // 压缩数据流 MemoryStream memStream = new MemoryStream(); GZipStream zipStream = null; try { // 创建压缩数据流 zipStream = new GZipStream(memStream, CompressionMode.Compress); // 从指定的字节数组中将压缩的字节写入基础流 zipStream.Write(fileBytes, 0, fileBytes.Length); } finally { if (zipStream != null) zipStream.Close(); // 关闭压缩数据流 memStream.Close(); } return memStream.ToArray(); } 等比例缩放图片<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();}} 重画Image oldImg = new Bitmap("d:/a.jpg");Image newImg = new Bitmap(oldImg.Width, oldImg.Height - 100);Graphics g = Graphics.FromImage(newImg);g.DrawImage(oldImg, new Point(0, 0)); 一个看似简单的不简单问题,求高手 C# MDI子窗体间传值 请教一个C#操作命令行的问题 c#项目,求高手 【求助】请问如何判断一个多边形是简单多边形还是复杂多边形? 求助!把下面VB的代码转换成C#的,万分感激! C#中如何绘制橡皮线,有没有绘图模式可用? 我有一事不明白,请各位达人给以解答. 问大虾一个SQL数据库查询效率的问题 C#中怎样做WORD文档增、删、改、存的web程序? ADO.NET EF 显示连接未关闭的问题 visual studio 2010 RC 与windows mobile 6.0 sdk 的问题~
http://topic.csdn.net/u/20100506/23/006f8270-4991-4279-ab20-0c274a76cb98.html?13579
/// <param name="fileByteArray">需要压缩的数据流</param>
/// <returns>返回压缩后的数据流</returns>
public static byte[] ZipFileStream(byte[] fileBytes)
{
if (fileBytes == null) return null;
// 压缩数据流
MemoryStream memStream = new MemoryStream();
GZipStream zipStream = null; try
{
// 创建压缩数据流
zipStream = new GZipStream(memStream, CompressionMode.Compress);
// 从指定的字节数组中将压缩的字节写入基础流
zipStream.Write(fileBytes, 0, fileBytes.Length);
}
finally
{
if (zipStream != null) zipStream.Close();
// 关闭压缩数据流
memStream.Close();
}
return memStream.ToArray();
}
<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();
}
}
重画
Image oldImg = new Bitmap("d:/a.jpg");
Image newImg = new Bitmap(oldImg.Width, oldImg.Height - 100);Graphics g = Graphics.FromImage(newImg);
g.DrawImage(oldImg, new Point(0, 0));