0

以下是从数据库中检索图像然后将其保存到文件夹的代码。

public string BinarytoNewsImage(int ID)
        {
            byte[] btimage = null;
            string image = "";
            string filename = null;
            int mediaid;

            DataSet dsNews = new DataSet();
            adp = new SqlDataAdapter("Select Top 1 * from tblNew Where intNewId=" + ID, offcon);
            adp.Fill(dsNews, "tblNews1");
            if (dsNews.Tables["tblNews1"].Rows.Count > 0)
            {
                if (dsNews.Tables["tblNews1"].Rows[0]["strImage"] != DBNull.Value)
                {
                    btimage = (byte[])dsNews.Tables["tblNews1"].Rows[0]["strImage"];
                    mediaid = Convert.ToInt32(dsNews.Tables["tblNews1"].Rows[0]["intMediaId"].ToString());
                    filename = dsNews.Tables["tblNews1"].Rows[0]["strfilename"].ToString();
                    image = BinarytoImage(btimage, mediaid);
                }
                else
                {
                    filename = dsNews.Tables["tblNews1"].Rows[0]["strfilename"].ToString();
                    image = "http://www.patrika.com/media/" + filename;
                }
            }

            return image;
        }

        public string BinarytoImage(byte[] stream, int ID)
        {
            string ImagePath = "";
            string Image = ID + ".jpg";

            var URL = System.Configuration.ConfigurationManager.AppSettings["ImagePath"].ToString();

            string FolderName = new Uri(URL).LocalPath;

            var help = HttpContext.Current.Server.MapPath(FolderName);

            if (Directory.Exists(HttpContext.Current.Server.MapPath(FolderName)))
            {
                string[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath(FolderName), ID + ".jpg");
                if (files.Length > 0)
                {
                    ImagePath = URL + ID + ".jpg";
                }
                else
                {
                    using (MemoryStream MS = new MemoryStream(stream, 0, stream.Length))
                    {
                        MS.Write(stream, 0, stream.Length);

                        System.Drawing.Image img = System.Drawing.Image.FromStream(MS);

                        img.Save(help + ID + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
                        img.Dispose();
                        img = null;
                        ImagePath = URL + ID + ".jpg";
                    }
                }
            }
            return ImagePath;
        }

一切正常,图像保存到文件夹中,但我的问题是图像在检索后变得模糊。

我只是不知道原因,因为当我使用另一个代码进行检索而不是图像正常但未保存到文件夹时:

DataSet dsNews = new DataSet();
            adp = new SqlDataAdapter("Select Top 1 * from tblNew Where intNewId=901371", con);
            adp.Fill(dsNews, "tblNews1");

            if (dsNews.Tables["tblNews1"].Rows[0]["strImage"] != DBNull.Value)
            {
                byte[] btimage = (byte[])dsNews.Tables["tblNews1"].Rows[0]["strImage"];
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite(btimage);
            }

我需要将这些图像保存到文件夹中,这样我就不必在图像出现后调用数据库。

4

2 回答 2

1

改变这条线不会有帮助吗

    img.Save(help + ID + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);

将其存储为JPEG?因为那是源格式

编辑: 您没有将流指针移回起点。

尝试更改这些行:

            using (MemoryStream MS = new MemoryStream(stream, 0, stream.Length))
                {
                    MS.Write(stream, 0, stream.Length);

                    System.Drawing.Image img = System.Drawing.Image.FromStream(MS);
             ...

            using (MemoryStream MS = new MemoryStream(stream, 0, stream.Length))
                {
                    MS.Write(stream, 0, stream.Length);
                    MS.Seek(0, SeekOrigin.Begin);
                    System.Drawing.Image img = System.Drawing.Image.FromStream(MS);

...

于 2013-01-04T13:37:48.240 回答
0

我写了常用的方法,一切正常。也许您的 byte[]stream 不正确,请检查。

 byte[] stream = File.ReadAllBytes(@"D:\YWG\123.jpg");
            using (MemoryStream MS = new MemoryStream(stream, 0, stream.Length))
            {
                MS.Write(stream, 0, stream.Length);

                using (Image img = Image.FromStream(MS))
                {
                    img.Save(@"D:\dd.jpg", System.Drawing.Imaging.ImageFormat.Gif);
                } 
            }

我看到dest文件“dd.jpg”没问题。

于 2013-01-04T13:58:48.807 回答