我正在尝试转换图像文件夹中的图像和图像名称defaultImage
并更新到我的数据库表中。
但是现在我在这行代码中遇到了问题:
我使用以下代码更改代码:
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
而不是:
System.Drawing.Image uploaded = System.Drawing.Image.FromStream(
〜/ images / defaultImage );
我现在收到FileNotFoundException was unhandled by user code
我已经尝试使用fileupload控件这个方法,它工作正常,但不知道如何转换文件夹中的图像。
如何从文件夹中获取图像,以便使用下面显示的方法进行转换。
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
//System.Drawing.Image uploaded = System.Drawing.Image.FromStream();
System.Drawing.Image newImage = new Bitmap(1024, 768);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(uploaded, 0, 0, 1024, 768);
}
byte[] results;
using (MemoryStream ms = new MemoryStream())
{
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters jpegParms = new EncoderParameters(1);
jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
newImage.Save(ms, codec, jpegParms);
results = ms.ToArray();
}
string sqlImage = "update MemberReport set image1 = @Data where memberreportid = '" + Session["memberreportid"] + "'";
SqlCommand cmdImage = new SqlCommand(sqlImage);
cmdImage.Parameters.AddWithValue("@Data", results);
InsertUpdateData(cmdImage);
答案 0 :(得分:2)
我猜你的问题是相对路径,而不是
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
你应该提供本地路径,你可以这样得到它:
Image uploaded6 = Image.FromFile(Server.MapPath("~/image/defaultImage.jpg"));
答案 1 :(得分:1)
System.Drawing.Image.FromStream需要'MemoryStream'作为参数。
byte[] file = null;
MemoryStream memoryStream = new MemoryStream();
memoryStream = new MemoryStream(file, false);
System.Drawing.Image objTempImg = System.Drawing.Image.FromStream(memoryStream)
byte []文件基于64图像
答案 2 :(得分:1)
System.Drawing.Image.FromStream(Stream)
您必须在此方法中发送Stream参数。