简体   繁体   English

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

[英]fastest way to create a jpeg from byte array c#

I have an image in an array and I'm saving it this way: 我在阵列中有一个图像,并且以这种方式保存它:

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

It works but I need it to be faster since its an image from camera that needs to stream. 它可以工作,但是我需要它更快,因为它需要来自流式摄像机的图像。 Is there a faster way? 有没有更快的方法? My array is in bytes then I use the container to convert to bitmap then save as jpeg . 我的数组以字节为单位,然后使用容器将其转换为位图,然后另存为jpeg

try this code, this works fast and efficient, in this piece of code you can convert a picture to JPEG specitfying the width and height, and can pass as many number of images as you want. 尝试使用此代码,它可以快速有效地工作,在这段代码中,您可以将图片转换为JPEG来指定宽度和高度,并可以传递任意数量的图像。 You can modify it to your own Requirement. 您可以将其修改为自己的需求。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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