简体   繁体   中英

Convert varbinary(max) to image,resize and save it in a folder using c# or vb.net?

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);

So far i am able to convert the varbinary(max) to image, and resized it.But not able to save it in a folder. Is this something to do with Bitmap? Any suggestions?

Hey add this line to save your image in folder

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM