欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# Bitmap转bitmapImage高效方法

程序员文章站 2022-03-23 09:58:12
网上有很多人都记录以下方法进行转换,这个方法存在一个问题,就是低效,我在进行图片拼接时,图片大了之后就会很慢。所以我有找了一个高效的替代方法。 public bitmapimage bitmaptob...

网上有很多人都记录以下方法进行转换,这个方法存在一个问题,就是低效,我在进行图片拼接时,图片大了之后就会很慢。所以我有找了一个高效的替代方法。

 public bitmapimage bitmaptobitmapimage(system.drawing.bitmap bitmap)
        {
            system.drawing.bitmap bitmapsource = new system.drawing.bitmap(bitmap.width, bitmap.height);
            int i, j;
            for (i = 0; i < bitmap.width; i++)
                for (j = 0; j < bitmap.height; j++)
                {
                    system.drawing.color pixelcolor = bitmap.getpixel(i, j);
                    system.drawing.color newcolor = system.drawing.color.fromargb(pixelcolor.r, pixelcolor.g, pixelcolor.b);
                    bitmapsource.setpixel(i, j, newcolor);
                }
            memorystream ms = new memorystream();
            bitmapsource.save(ms, system.drawing.imaging.imageformat.bmp);
            bitmapimage bitmapimage = new bitmapimage();
            bitmapimage.begininit();
            bitmapimage.streamsource = new memorystream(ms.toarray());
            bitmapimage.endinit();
 
            return bitmapimage;
        }

这种方法快多了。赶紧快去试试吧:

public bitmapimage tobitmapimage(system.drawing.bitmap imageoriginal)
        {
 
            system.drawing.bitmap imageoriginalbase = new system.drawing.bitmap(imageoriginal);
            bitmapimage bitmapimage = new bitmapimage();
            using (system.io.memorystream ms = new system.io.memorystream())
            {
                imageoriginalbase.save(ms, system.drawing.imaging.imageformat.png);
                bitmapimage.begininit();
                bitmapimage.streamsource = ms;
                bitmapimage.cacheoption = bitmapcacheoption.onload;
                bitmapimage.endinit();
                bitmapimage.freeze();
            }
            return bitmapimage;
        }

补:c# byte[]、image、bitmap 之间的相互转换

/// <summary>  
        /// 将图片image转换成byte[]  
        /// </summary>  
        /// <param name="image">image对象</param>  
        /// <param name="imageformat">后缀名</param>  
        /// <returns></returns>  
        public static byte[] imagetobytes(image image, system.drawing.imaging.imageformat imageformat)  
        {  
  
            if (image == null) { return null; }  
  
            byte[] data = http://www.cnblogs.com/peasana/archive/2012/02/13/null;  
  
            using (memorystream ms= new memorystream())  
            {  
  
                 using (bitmap bitmap = new bitmap(image))  
                {  
  
                    bitmap.save(ms, imageformat);  
  
                    ms.position = 0;  
  
                    data = http://www.cnblogs.com/peasana/archive/2012/02/13/new byte[ms.length];  
  
                    ms.read(data, 0, convert.toint32(ms.length));  
  
                    ms.flush();  
  
                }  
  
            }  
  
            return data;  
  
        }  

            /// <summary>  
            /// byte[]转换成image  
            /// </summary>  
            /// <param name="bytearrayin">二进制图片流</param>  
            /// <returns>image</returns>  
            public static system.drawing.image bytearraytoimage(byte[] bytearrayin)  
            {  
                if (bytearrayin == null)  
                    return null;  
                using (system.io.memorystream ms = new system.io.memorystream(bytearrayin))  
                {  
                    system.drawing.image returnimage = system.drawing.image.fromstream(ms);  
                    ms.flush();  
                    return returnimage;  
                }  
            }    
  
    //image转换bitmap  
  
   1. bitmap img = new bitmap(imgselect.image);  
  
   2. bitmap bmp = (bitmap)picturebox1.image;  
  
     
//bitmap转换成image  
  
using system.io;  
  
private static system.windows.controls.image bitmap2image(system.drawing.bitmap bi)  
        {             
            memorystream ms = new memorystream();  
            bi.save(ms, system.drawing.imaging.imageformat.png);  
            bitmapimage bimage = new bitmapimage();  
            bimage.begininit();  
            bimage.streamsource = new memorystream(ms.toarray());  
            bimage.endinit();  
            ms.dispose();  
            bi.dispose();  
            system.windows.controls.image i = new system.windows.controls.image();  
            i.source = bimage;  
            return i ;  
        }    
  
//byte[] 转换 bitmap  
 public static bitmap bytestobitmap(byte[] bytes)   
        {   
            memorystream stream = null;   
            try   
            {   
                stream = new memorystream(bytes);   
                return new bitmap((image)new bitmap(stream));   
            }   
            catch (argumentnullexception ex)   
            {   
                throw ex;   
            }   
            catch (argumentexception ex)   
            {   
                throw ex;   
            }   
            finally   
            {   
                stream.close();   
            }   
        }    
   
//bitmap转byte[]    
        public static byte[] bitmaptobytes(bitmap bitmap)   
        {   
            memorystream ms = null;   
            try   
            {   
                ms = new memorystream();   
                bitmap.save(ms, bitmap.rawformat);   
                byte[] byteimage = new byte[ms.length];   
                byteimage = ms.toarray();   
                return byteimage;   
            }   
            catch (argumentnullexception ex)   
            {   
                throw ex;   
            }   
            finally   
            {   
                ms.close();   
            }   
        }   
    }   

到此这篇关于c# bitmap 转bitmapimage高效方法的文章就介绍到这了,更多相关c# bitmap 转bitmapimage内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!