2016-09-15 5 views
0

コードは私が見たことがあるがチュートリアルでは動作しません。 は、ここに私のコードです:C#バイトをイメージに変換できません

private void AddButton() 
{   
    foreach (TblProductType category in cse.TblProductType) 
    { 
     Button btn = new Button(); 
     btn.Text = category.Description; 
     btn.Size = new Size(100, 100); 
     btn.ForeColor = Color.White; 

     byte [] dataCategory1 = category.Productimage; 
     MemoryStream stm = new MemoryStream(dataCategory1); 
     btn.Image = Image.FromStream(stm); 
     btn.Image = ResizeImage(btn.Image, btn.Size); 

     btn.Tag = category.ProductTypes; 
     flow1.Controls.Add(btn); 
     this.Controls.Add(flow1); 
     btn.Click += btn_Click 
    } 
} 
+3

Hey Samuelさん、こんにちは。構文エラーの場合や単にイメージを作成しない場合のように、問題に関するさらに詳しい情報を提供する必要があります。 –

+0

あなたはResizeImageのコードを共有してください。 –

+0

_働いていませんが役立つ問題の説明ではありません! – TaW

答えて

0

私たちはあなたの問題の正確な性質にもう少し周囲のコード、またはより多くの情報が必要かもしれませんが、私はこの問題の原因として最も可能性が高いがResizeImageで起こっているものは何でもある疑い。

System.Drawing.Imageのサイズを変更する機能はありますか?恐らくそれを試して問題が解決したかどうかを確認してください。ここで

public static Image ResizeImage(int newWidth, int newHeight, Image image) { 

     int sourceWidth = image.Width; 
     int sourceHeight = image.Height; 

     //Consider vertical pics 
     if (sourceWidth < sourceHeight) { 
      int buff = newWidth; 

      newWidth = newHeight; 
      newHeight = buff; 
     } 

     int sourceX = 0, sourceY = 0, destX = 0, destY = 0; 
     float percent = 0, percentW = 0, percentH = 0; 

     percentW = ((float)newWidth/(float)sourceWidth); 
     percentH = ((float)newHeight/(float)sourceHeight); 
     if (percentH < percentW) { 
      percent = percentH; 
      destX = System.Convert.ToInt16((newWidth - 
         (sourceWidth * percent))/2); 
     } else { 
      percent = percentW; 
      destY = System.Convert.ToInt16((newHeight - 
         (sourceHeight * percent))/2); 
     } 

     int destWidth = (int)(sourceWidth * percent); 
     int destHeight = (int)(sourceHeight * percent); 


     Bitmap bitmap = new Bitmap(newWidth, newHeight, 
         PixelFormat.Format24bppRgb); 

     bitmap.SetResolution(image.HorizontalResolution, 
        image.VerticalResolution); 

     Graphics graphic = Graphics.FromImage(bitmap); 
     graphic.Clear(Color.Black); 
     graphic.InterpolationMode = 
      System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 

     graphic.DrawImage(image, 
      new Rectangle(destX, destY, destWidth, destHeight), 
      new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
      GraphicsUnit.Pixel); 

     graphic.Dispose(); 
     image.Dispose(); 
     return bitmap; 
    } 
0

、これを試してみてください。 私の問題では、私はMemoryStreamをへの私のチャートを保存...その後、MemoryStreamを画像にバイト配列を保存した後、配列をバイトとすることを保存しました。わたしにはできる。

using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      //saved my chart to an memoryStream 
      Chart2.SaveImage(memoryStream, ChartImageFormat.Png); 

      //saved memorystream to byte array 
      byte[] byteArrayIn = memoryStream.ToArray(); 

      //saving byte back to an Image 
      Image image1 = new Image(); 
      image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteArrayIn, 0, byteArrayIn.Length); 

      newChart.Controls.Add(image1); 
      divRadarChart.Visible = false; 
     }