2016-01-08 118 views
-1

我想結合兩個圖像,但我不能成功。請幫幫我。將兩個圖像添加到一個圖像c#?

當嘗試組合PictureBox只顯示第一個圖像,但沒有第二個圖像,當我刪除第一個圖像時,我可以看到第二個圖像。

此外,我試圖設置第一個圖像,並在圖像上繪製文本也不工作。請幫忙。

Image myimg = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true); 

Bitmap image = new Bitmap(myimg.Width + 20, myimg.Height + 50); 
pictureBox1.DrawToBitmap(image, new Rectangle(0, 0, myimg.Width + 20, myimg.Height + 50)); 
Bitmap bmp = new Bitmap(myimg.Width + 20, myimg.Height); 
Bitmap bmp2 = new Bitmap(myimg.Width + 20, 20); 
Graphics Cizgi2 = Graphics.FromImage(bmp2); 
Graphics Cizgi = Graphics.FromImage(bmp); 
Cizgi.DrawImage(myimg, 0, 0); 

FontStyle sitil = FontStyle.Bold; 
Font fonts = new Font(new FontFamily("Arial"), 10, sitil); 
Cizgi2.DrawString(textBox1.Text, fonts, Brushes.Black, 5, myimg.Height + 10); 

Graphics g = Graphics.FromImage(image); 
g.DrawImage(bmp, new Point(10, 0)); 
g.DrawImage(bmp2, new Point(0, bmp.Height + 10)); 

I want to image seems like first but i cant make

+0

您想同時顯示兩個圖像還是想要在另一個圖像的頂部添加一個圖像? – Jamiec

+2

請定義你想如何組合圖像。並排,下/上或以某種方式疊加? – ChrisF

+0

你真的應該更好地解釋你想做什麼,並回答我們問的問題!結合兩個圖像很簡單,4-5行代碼和diplaying結果需要多一行.. – TaW

回答

1

它看起來像你試圖垂直連接兩個圖像?實際上這很簡單,你可以看這裏(C# image concatenation),但我也修改它以滿足你的需求。我想,這應該工作:

 float drawBorderX = 5; 
     float drawBorderY = 5; 

     //Set up our two images 
     Bitmap barCode = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true); 
     Bitmap text = new Bitmap(barCode.Width, 50); 
     Graphics textGraphics = Graphics.FromImage(text); 


     //Draw the text to the bottom image. 
     FontStyle sitil = FontStyle.Bold; 
     Font fonts = new Font(new FontFamily("Arial"), 10, sitil); 
     textGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, text.Width, text.Height)); 
     textGraphics.DrawString(textBox1.Text, fonts, Brushes.Black, drawBorderX, drawBorderY); 

     //Vertically concatenate the two images. 
     Bitmap resultImage = new Bitmap(Math.Max(barCode.Width, text.Width), barCode.Height + text.Height); 
     Graphics g = Graphics.FromImage(resultImage); 
     g.DrawImage(barCode, 0, 0); 
     g.DrawImage(text, 0, barCode.Height); 

編輯:請注意,resultImage將包含你想要的形象,所以你可以設置你的PictureBox是在年底的形象。

+0

它的作品。我只是添加位圖barCode =(位圖)Code128Rendering.MakeBarcodeImage(textBox1.Text,2,true); –