将varbinary(max)转换为image,调整大小并使用c#或vb.net将其保存在文件夹中?

时间:2013-07-22 09:17:47

标签: c# asp.net vb.net c#-4.0 c#-3.0

public static BinaryToImage(System.Data.Linq.Binary binaryData)
{
    if (binaryData == null) {
    return null;
    }
    byte[] buffer = binaryData.ToArray();
    System.Drawing.Image newImage = default(System.Drawing.Image);
    MemoryStream memStream = new MemoryStream();
    memStream.Write(buffer, 0, buffer.Length);
    using (MemoryStream strefgham = new MemoryStream(buffer)) {
       newImage = System.Drawing.Image.FromStream(strefgham);
       return newImage;
    }
}
public static double GetPercent(double width,double height,int originalWidth,int originalHeight)
 {
    if (width <= originalWidth && height <= originalHeight) {
    return 1.0;
    } else 
        {
    double wid = (originalWidth / width);
    double hei = (originalHeight / height);
    return (wid < hei) ? wid : hei;
   }
}


   System.Drawing.Image newImage = default(System.Drawing.Image);
   newImage = BinaryToImage(VarBinaryName.ToArray());
   double perc = GetPercent(newImage.Width, newImage.Height, 300, 300);
   double newWidth = newImage.Width * perc;
   double newHeight = newImage.Height * perc;
   int disWeight = Convert.ToInt32(newWidth);
   int disHeight = Convert.ToInt32(newHeight);

到目前为止,我能够将varbinary(max)转换为图像,并将其调整大小。但是无法将其保存在文件夹中。这与Bitmap有关吗?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

嘿,添加此行可将图像保存到文件夹

if (!Directory.Exists("D:\Test"))
        {
            Directory.CreateDirectory("D:\Test");
        }
newImage.Save(@"D:\Test\New.jpg", origImage.RawFormat);