使用asp.net c#调整图像大小 [英] Resizing image using asp.net c#
问题描述
i编写了一个代码,用于保存原始图像并使用asp.net c#在数据库中保存已调整大小的图像。
string imgName = FileUpload1.FileName.ToString();
//设置图像路径
string imgPath =〜/ upload /+ imgName;
string imgPath2 =〜/ small upload /+ imgName;
//获取
filepath = Server.MapPath(imgPath);
filepath2 = Server.MapPath(imgPath2);
FileUpload1.SaveAs(Server.MapPath(imgPath));
ResizeImage(30,filepath,filepathsmall);
FileStream fs = new FileStream(filepath2,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte [] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
string strQuery =INSERT INTO产品(名称,描述,图像,类别,价格,图像路径)VALUES(@NM,@ EM,@ PN,@ CT,@ PC,@ IP);
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue(@ NM,name);
cmd.Parameters.AddWithValue(@ EM,desc);
cmd.Parameters.AddWithValue(@ PN,bytes);
cmd.Parameters.AddWithValue(@ CT,类别);
cmd.Parameters.AddWithValue(@ PC,价格);
cmd.Parameters.AddWithValue(@ IP,imgPath2);
InsertData(cmd);
函数
public static void ResizeImage(int size,string filePath,string saveFilePath)
{
//图像尺寸/比例变量
double newHeight = 0;
double newWidth = 0;
double scale = 0;
//创建新图像对象
位图curImage = new Bitmap(filePath);
//确定图像缩放
if(curImage.Height> curImage.Width)
{
scale = Convert.ToSingle(size)/ curImage.Height;
}
其他
{
scale = Convert.ToSingle(size)/ curImage.Width;
}
if(scale< 0 || scale> 1){scale = 1; }
//新图像维度
newHeight = Math.Floor(Convert.ToSingle(curImage.Height)* scale);
newWidth = Math.Floor(Convert.ToSingle(curImage.Width)* scale);
//创建新对象图像
Bitmap newImage = new Bitmap(curImage,Convert.ToInt32(newWidth),Convert.ToInt32(newHeight));
Graphics imgDest = Graphics.FromImage(newImage);
imgDest.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
imgDest.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
imgDest.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
imgDest.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
ImageCodecInfo [] info = ImageCodecInfo.GetImageEncoders();
EncoderParameters param = new EncoderParameters(1);
param.Param [0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,100L);
//绘制对象图像
imgDest.DrawImage(curImage,0,0,newImage.Width,newImage.Height);
//保存图像文件
newImage.Save(saveFilePath,info [1],param);
//处理图像对象
curImage.Dispose();
newImage.Dispose();
imgDest.Dispose();
}
你能告诉我我们应该以哪种格式给出尺寸因子。当我编译时我得到错误
GDI +中发生了一般性错误。
你可以帮我解决这个问题吗?或者建议我另一个代码
检查一次......
http://stackoverflow.com/questions/9783999/how-to-resize-image-in-asp-net-with-c-application-in-优化方式 [ ^ ]
试试这个链接:
http://forums.asp.net/t/1851762。 aspx?c + Resize + image +和+ upload + to + db [ ^ ]
使用C#动态调整图像大小 [ ^ ]
原因
Quote:当构造Bitmap对象或Image对象时从文件中,文件在对象的生命周期内保持锁定状态。因此,您无法更改图像并将其保存回报价参考
所在的同一文件中
a> [ ^ ]
解决方案,请参阅以下链接:
参考 [ ^ ]
Hi,
i have written a code for saving original image and saving resized image in database using asp.net c#.
string imgName = FileUpload1.FileName.ToString(); //sets the image path string imgPath = "~/upload/" + imgName; string imgPath2 = "~/small upload/" + imgName; //get the size in bytes that filepath = Server.MapPath(imgPath); filepath2 = Server.MapPath(imgPath2); FileUpload1.SaveAs(Server.MapPath(imgPath)); ResizeImage(30,filepath,filepathsmall); FileStream fs = new FileStream(filepath2, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); br.Close(); fs.Close(); string strQuery = "INSERT INTO products(name, description, image,category,price,imagepath) VALUES(@NM, @EM, @PN,@CT,@PC,@IP)"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.AddWithValue("@NM", name); cmd.Parameters.AddWithValue("@EM", desc); cmd.Parameters.AddWithValue("@PN", bytes); cmd.Parameters.AddWithValue("@CT", category); cmd.Parameters.AddWithValue("@PC", price); cmd.Parameters.AddWithValue("@IP", imgPath2); InsertData(cmd); Functions public static void ResizeImage(int size, string filePath, string saveFilePath) { //variables for image dimension/scale double newHeight = 0; double newWidth = 0; double scale = 0; //create new image object Bitmap curImage = new Bitmap(filePath); //Determine image scaling if (curImage.Height > curImage.Width) { scale = Convert.ToSingle(size) / curImage.Height; } else { scale = Convert.ToSingle(size) / curImage.Width; } if (scale < 0 || scale > 1) { scale = 1; } //New image dimension newHeight = Math.Floor(Convert.ToSingle(curImage.Height) * scale); newWidth = Math.Floor(Convert.ToSingle(curImage.Width) * scale); //Create new object image Bitmap newImage = new Bitmap(curImage, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight)); Graphics imgDest = Graphics.FromImage(newImage); imgDest.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; imgDest.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; imgDest.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; imgDest.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders(); EncoderParameters param = new EncoderParameters(1); param.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); //Draw the object image imgDest.DrawImage(curImage, 0, 0, newImage.Width, newImage.Height); //Save image file newImage.Save(saveFilePath, info[1], param); //Dispose the image objects curImage.Dispose(); newImage.Dispose(); imgDest.Dispose(); }
can you tell me in which format should we give size factor.when i compile i am getting error
A generic error occurred in GDI+.
can you help me solve this eeror or suggest me another code
Check it once...
http://stackoverflow.com/questions/9783999/how-to-resize-image-in-asp-net-with-c-application-in-optimizable-way[^]
Try this link:
http://forums.asp.net/t/1851762.aspx?c+Resize+image+and+upload+to+db[^]
Resizing image dynamically using C#[^]
Reason
Quote:When either a Bitmap object or an Image object is constructed from a file, the file remains locked for the lifetime of the object. As a result, you cannot change an image and save it back to the same file where it originated
from Quote Reference[^]
Solution,refer below link :
Refer[^]
这篇关于使用asp.net c#调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!