2012-03-26 101 views
0

我試圖將我的字符串轉換爲173x173 png圖像。這是否有可能,如果是的話:怎麼樣?之後需要使用網址將其用於我的腰部。將字符串轉換爲圖像

字符串可以包含字母,數字和 「 - /。」

發現類似的東西,但似乎並沒有在所有的工作:

private Uri ToImage() 
    { 
     string imageString = ""; 

     byte[] imageBytes = Convert.FromBase64String(imageString); 
     System.Text.Encoding.UTF8.GetBytes(imageString.ToCharArray(), 0, imageString.ToCharArray().Length - 1, 
     imageBytes, 0); 

     System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBytes, 0, imageBytes.Length); 

     ms.Write(imageBytes, 0, imageBytes.Length); 

     BitmapImage bitmapImage = new BitmapImage(); 
     bitmapImage.SetSource(ms); 
     return bitmapImage.UriSource; 
    } 
+0

有人明白這個代碼,據我瞭解嗎? :) – Ku6opr 2012-03-26 14:58:51

+0

這是你想要的嗎? http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx – 2012-03-26 15:09:41

+0

我不知道他是否得到了一個字符串中的編碼圖像,或者他正在試圖寫一個字符串到平鋪圖像。哪一個? – Robaticus 2012-03-26 15:27:59

回答

1

要加載圖像FOM一個base64字符串你可以使用它:

Image img; 
byte[] fileBytes = Convert.FromBase64String(imageString); 
using(MemoryStream ms = new MemoryStream()) 
{ 
    ms.Write(fileBytes, 0, fileBytes.Length); 
    img = Image.FromStream(ms); 
} 

您可以在服務器上保存此圖像和發送文件的URL到客戶端或發送「上即時」的形象:

Response.ContentType = "image/png"; 
img.Save(Response.OutputStream, ImageFormat.Png); 

在這種情況下,圖像URL是頁面的URL,例:

img src="getBacktile.aspx?id=XXX" 
+0

我不確定這個答案是他想要完成的。儘管如此,說實話,我不確定這裏的目標是什麼。無論如何,要考慮的一件事就是PNG通常不是一個好的選擇,因爲它的大小可以超過平鋪圖像的最大尺寸。我通常使用JPG,因爲在手機上,格式的任何有損性通常都會被較小的顯示大小所掩蓋。 – Robaticus 2012-03-26 15:31:12

+0

jpg不支持透明度,多數民衆贊成在問題。 – roqstr 2012-03-26 15:43:43

+0

這是錯誤的。 Windows Phone沒有Image.FromStream方法。 – zi3guw 2013-01-26 16:24:22

0

只是刪除的公共抽象INT GetBytes會(的char []字符/註釋行代碼,INT的charIndex,INT charCount ,byte [] bytes,int byteIndex);

System.Text.Encoding.UTF8.GetBytes(imageString.ToCharArray(), 0, imageString.ToCharArray().Length - 1, 
     imageBytes, 0); 

它對我有用。

而更多的東西,轉換使用行代碼

Image img; 
byte[] fileBytes = Convert.FromBase64String(imageString); 
using(MemoryStream ms = new MemoryStream()) 
{ 
    ms.Write(fileBytes, 0, fileBytes.Length); 
    img = Image.FromStream(ms); 
} 

不起作用。由於在Windows Phone 7中不提供方法Image.FromStream(ms)