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有关吗?有什么建议吗?
答案 0 :(得分:0)
嘿,添加此行可将图像保存到文件夹
if (!Directory.Exists("D:\Test"))
{
Directory.CreateDirectory("D:\Test");
}
newImage.Save(@"D:\Test\New.jpg", origImage.RawFormat);