C# Image image = Image.FromStream()报错参数无效
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Image image = Image.FromStream()报错参数无效相关的知识,希望对你有一定的参考价值。
我将pictureBox1控件中的图片保存到SQL server2005 数据库中,然后我再读出来,但是在 Image image = Image.FromStream(mys);这句话上报错,参数无效,哪位大神能解答一下?谢谢了!
第一张是向数据库存图片的代码,第二张是读数据库图片的代码,读的时候报的错。
Image image = Image.FromStream(mys);这句话上报错
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save("a.bmp");
FileStream filestream = new FileStream("a.bmp", FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(filestream);
byte[] img = binaryReader.ReadBytes((int)filestream.Length);
DBHelper.ExecuteCommand("insert into kehuxinxi values('" + img + "','文字描述')");
filestream.Close();
binaryReader.Close();
参数改为这个试试
System.Text.Encoding.UTF8.GetBytes(tupian.Rows[0]["zhaopian"])
改为这个试试. 参考技术B 存的时候就有问题,你的字段是什么类型?追问
您好,
我的字段是image类型的
你把保存时的代码,帖文本上来
追问好了 您看看
追答MemoryStream ms = new MemoryStream();pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] img = ms.ToArray();
SqlParameter p=new SqlParameter();
p.ParameterName="@img";
p.SqlDbType=SqlDbType.Image;
p.Value =img;
DBHelper.ExecuteCommand("insert into kehuxinxi values(@img,'文字描述')",p);//你这个DBHelper类,应该是可以加这个一个SqlParameter类型的参数。
ms.Close();追问
高手啊 ,解决了 谢谢啊!
本回答被提问者采纳C#公共帮助类 Image帮助类
大家知道,开发项目除了数据访问层很重要外,就是Common了,这里就提供了强大且实用的工具。
Image类:
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing.Imaging; using System.Drawing; using System.Web; namespace Common { public static class Image { #region 图片格式转换 /// <summary> /// 图片格式转换 /// </summary> /// <param name="OriFilename">原始文件相对路径</param> /// <param name="DesiredFilename">生成目标文件相对路径</param> /// <returns></returns> /// JPG采用的是有损压缩所以JPG图像有可能会降低图像清晰度,而像素是不会降低的 /// GIF采用的是无损压缩所以GIF图像是不会降低原图图像清晰度和像素的,但是GIF格式只支持256色图像。 public static bool ConvertImage(string OriFilename, string DesiredFilename) { string extname = DesiredFilename.Substring(DesiredFilename.LastIndexOf(‘.‘)+1).ToLower(); ImageFormat DesiredFormat; //根据扩张名,指定ImageFormat switch (extname) { case "bmp": DesiredFormat = ImageFormat.Bmp; break; case "gif": DesiredFormat = ImageFormat.Gif; break; case "jpeg": DesiredFormat = ImageFormat.Jpeg; break; case "ico": DesiredFormat = ImageFormat.Icon; break; case "png": DesiredFormat = ImageFormat.Png; break; default: DesiredFormat = ImageFormat.Jpeg; break; } try { System.Drawing.Image imgFile = System.Drawing.Image.FromFile(WebPathTran(OriFilename)); imgFile.Save(WebPathTran(DesiredFilename), DesiredFormat); return true; } catch { return false; } } #endregion #region 图片缩放 /// <summary> /// 图片固定大小缩放 /// </summary> /// <param name="OriFileName">源文件相对地址</param> /// <param name="DesiredFilename">目标文件相对地址</param> /// <param name="IntWidth">目标文件宽</param> /// <param name="IntHeight">目标文件高</param> /// <param name="imageFormat">图片文件格式</param> public static bool ChangeImageSize(string OriFileName, string DesiredFilename, int IntWidth, int IntHeight, ImageFormat imageFormat) { string SourceFileNameStr =WebPathTran(OriFileName); //来源图片名称路径 string TransferFileNameStr = WebPathTran(DesiredFilename); //目的图片名称路径 FileStream myOutput =null; try { System.Drawing.Image.GetThumbnailImageAbort myAbort = new System.Drawing.Image.GetThumbnailImageAbort(imageAbort); Image SourceImage = System.Drawing.Image.FromFile(OriFileName);//来源图片定义 Image TargetImage = SourceImage.GetThumbnailImage(IntWidth, IntHeight, myAbort, IntPtr.Zero); //目的图片定义 //将TargetFileNameStr的图片放宽为IntWidth,高为IntHeight myOutput = new FileStream(TransferFileNameStr, FileMode.Create, FileAccess.Write, FileShare.Write); TargetImage.Save(myOutput, imageFormat); myOutput.Close(); return true; } catch { myOutput.Close(); return false; } } private static bool imageAbort() { return false; } #endregion #region 文字水印 /// <summary> /// 文字水印 /// </summary> /// <param name="wtext">水印文字</param> /// <param name="source">原图片物理文件名</param> /// <param name="target">生成图片物理文件名</param> public static bool ImageWaterText(string wtext,string source, string target) { bool resFlag = false; Image image = Image.FromFile(source); Graphics graphics = Graphics.FromImage(image); try { graphics.DrawImage(image, 0, 0, image.Width, image.Height); Font font = new System.Drawing.Font("Verdana", 60); Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green); graphics.DrawString(wtext, font, brush, 35, 35); image.Save(target); resFlag = true; } catch (Exception) { throw; } finally { graphics.Dispose(); image.Dispose(); } return resFlag; } #endregion #region 图片水印 /// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name="Path">原服务器图片路径</param> /// <param name="Path_syp">生成的带图片水印的图片路径</param> /// <param name="Path_sypf">水印图片路径</param> public static bool ImageWaterPic(string source, string target, string waterPicSource) { bool resFlag = false; Image sourceimage = Image.FromFile(source); Graphics sourcegraphics = Graphics.FromImage(sourceimage); Image waterPicSourceImage = Image.FromFile(waterPicSource); try { sourcegraphics.DrawImage(waterPicSourceImage, new System.Drawing.Rectangle(sourceimage.Width - waterPicSourceImage.Width, sourceimage.Height - waterPicSourceImage.Height, waterPicSourceImage.Width, waterPicSourceImage.Height), 0, 0, waterPicSourceImage.Width, waterPicSourceImage.Height, GraphicsUnit.Pixel); sourceimage.Save(target); } catch (Exception) { throw; } finally { sourcegraphics.Dispose(); sourceimage.Dispose(); waterPicSourceImage.Dispose(); } return resFlag; } #endregion /// <summary> /// 路径转换(转换成绝对路径) /// </summary> /// <param name="path"></param> /// <returns></returns> private static string WebPathTran(string path) { try { return HttpContext.Current.Server.MapPath(path); } catch { return path; } } } }
以上是关于C# Image image = Image.FromStream()报错参数无效的主要内容,如果未能解决你的问题,请参考以下文章