当前位置:编程学习 > C#/ASP.NET >>

如何轉換保存圖片?很報歉我分不多

  實現目的:把一個圖片及一些參數轉成字符串,發送到遠程,然后由遠程電腦根據參數來處理這個由字符串轉化得來的圖片。

 
 public static byte 圖片轉BYTE數組(Bitmap A_0)
        {
            if (A_0 == null)
            {
                return null;
            }
            BitmapData bitmapdata = A_0.LockBits(new Rectangle(new System.Drawing.Point(), A_0.Size), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            int length = bitmapdata.Stride * A_0.Height;
            byte destination = new byte;
            Marshal.Copy(bitmapdata.Scan0, destination, 0, length);
            A_0.UnlockBits(bitmapdata);
            return destination;
        }

      
        public static string BYTE數組轉字符串(byte A_0)
        {
            //string str = System.Text.Encoding.Unicode.GetString(A_0);
            if (A_0 == null)
            {
                return "";
            }
            string str = "";
            if (A_0 != null)
            {
                for (int i = 0; i < A_0.Length; i++)
                {
                    str = str + A_0.ToString("X2");
                }
            }
            return str;
        }

用法:A_為一個圖片
 string str = string.Concat(new object
                      {
                           BYTE數組轉字符串(圖片轉BYTE數組(a_)), 
                           "|", 
                             a_.Width, 
                           "|", 
                            a_.Height
                      });


如果我得到了這個字符串,我如何轉化成圖片呢?還有就是 BYTE數組轉字符串這個數組與字符串轉化過程中可能很費時間,有沒有更好的處理方法。 --------------------编程问答-------------------- 从二进制转换成字符串,可以使用Convert.ToBase64String().反向使用Convert.FromBase64String().

性能上就不知道比你的好还是不好了。

你的代码不是C#的吧。C#应该用byte[]而不是byte. --------------------编程问答-------------------- 从byte[]取得图片可以用下面的代码:

public static Bitmap BytesToBitmap(byte[] byteArray)
{
using (MemoryStream ms = new MemoryStream(byteArray))
{
Bitmap img = (Bitmap)Image.FromStream(ms);
return img;
}
}
--------------------编程问答-------------------- 是C#,少打[]。
试过,数组转图片报错 --------------------编程问答-------------------- 我们一直这样用啊,没问题,报舍什么错误。

或者试一试:
Code Block
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(byteArray);
Or,
Code Block
ImageConverter ic = new ImageConverter();
Image img = (Image)ic.ConvertFrom(byteArray);
Bitmap bitmap1 = new Bitmap(img);

确定你的byte[]是正确的。 --------------------编程问答-------------------- 你怎么把图片转数组? --------------------编程问答-------------------- 其它方式转报“GDI+错误”, --------------------编程问答-------------------- c#中没必要用这样的循环吧
 for (int i = 0; i < A_0.Length; i++) 

      str = str + A_0.ToString("X2"); 


c#中应该有相关的函数来处理吧,用c#带的函数应该效率会高点

--------------------编程问答-------------------- 除 --------------------编程问答-------------------- 圖片轉BYTE數組(Bitmap A_0),转化不了

ImageConverter ic = new ImageConverter();
Image img = (Image)ic.ConvertFrom(byteArray);
Bitmap bitmap1 = new Bitmap(img); --------------------编程问答-------------------- Bitmap a_ = new Bitmap(stream); 
System.IO.MemoryStream ms = new System.IO.MemoryStream();
a_.Save(ms, ImageFormat.Bmp);//GDI+ 中发生一般性错误。
byte[] byteImage = new Byte[0];
byteArray = ms.ToArray();
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,