从字节数组c#创建jpeg的最快方法

时间:2011-03-29 07:16:59

标签: c# image-processing

我有一个数组中的图像,我用这种方式保存它:

ImageConverter ic = new ImageConverter();
Image img = (Image)ic.ConvertFrom(Jpeg);
Bitmap bitmap1 = new Bitmap(img);
string saveString = "c:\\M_files\\new_pics\\" + pictureCounter + ".jpg";
bitmap1.Save(saveString, System.Drawing.Imaging.ImageFormat.Jpeg);

它可以工作,但我需要它更快,因为它是一个需要流的相机图像。有更快的方法吗?我的数组以字节为单位,然后我使用容器转换为位图,然后保存为 jpeg

1 个答案:

答案 0 :(得分:0)

尝试此代码,这可以快速有效地工作,在这段代码中,您可以将图片转换为JPEG,指定宽度和高度,并可以根据需要传递尽可能多的图像。您可以将其修改为您自己的要求。

public void CreateThumbnail(string[] b, double wid, double hght, bool Isprint)
{
    string[] path;
    path = new string [64];
    path = b;
    string saveath = "i:\\check\\a test\\";
    for (int i = 0; i < b.Length; i++)
    {
        DirectoryInfo dir = new DirectoryInfo(path[i]);
        string dir1 = dir.ToString();
        dir1 = dir1.Substring(dir1.LastIndexOf("\\"));

    FileInfo[] files1 = dir.GetFiles();

    foreach (FileInfo f in files1)
    {
        string gh = f.ToString();
        try
        {
            System.Drawing.Image myThumbnail150;
            System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            System.Drawing.Image imagesize = System.Drawing.Image.FromFile(f.FullName);
            Bitmap bitmapNew = new Bitmap(imagesize);
            double maxWidth = wid;
            double maxHeight = hght;
            int w = imagesize.Width;
            int h = imagesize.Height;
            // Longest and shortest dimension 
            int longestDimension = (w > h) ? w : h;
            int shortestDimension = (w < h) ? w : h;
            // propotionality  
            float factor = ((float)longestDimension) / shortestDimension;
            // default width is greater than height    
            double newWidth = maxWidth;
            double newHeight = maxWidth / factor;
            // if height greater than width recalculate  
            if (w < h)
            {
                newWidth = maxHeight / factor;
                newHeight = maxHeight;
            }
            myThumbnail150 = bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight, myCallback, IntPtr.Zero);

            string ext = Path.GetExtension(f.Name);

            if (!Directory.Exists(saveath + dir1))
            {
                Directory.CreateDirectory(saveath + dir1);
                myThumbnail150.Save(saveath + dir1 + "\\" + f.Name.Replace(ext, ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            else if(Directory.Exists(saveath+dir1))
            {
                myThumbnail150.Save(saveath + dir1+" \\"+ f.Name.Replace(ext, ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("something went wrong" + ex.ToString());
        }
    }
}

}