2011-03-29 43 views
0

我正在嘗試調整.NET位圖的大小而不實際縮放圖像。這裏的想法是在圖像上方和下方創建一個空間,用黑色矩形填充它,並在其中放置一些文本(不會覆蓋或破壞原始圖像的任何部分)。在VB.NET中添加文本BELOW位圖

到目前爲止,我所見過的所有代碼和示例都展示瞭如何縮放圖像,而不是展開畫布。到目前爲止,我嘗試過的所有東西都可以縮放/拉伸圖像。

這是我有:

Dim imageSize As System.Drawing.Size 
Dim exifImage As Image 

exifImage = System.Drawing.Image.FromFile(originalPath) 

imageSize.Height = exifImage.Height 
imageSize.Width = exifImage.Width 
imageSize.Height += BLAH '' whatever I need to add to my height for the text 
'' this doesn't work, because the image gets stretched instead of stuff getting added above and below 

Dim exifOverlayImage As New System.Drawing.Bitmap(exifImage, imageSize) 

Dim graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(exifOverlayImage) 
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic 
graphic.SmoothingMode = SmoothingMode.HighQuality 
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality 
graphic.CompositingQuality = CompositingQuality.HighQuality 

'' Draw Title at the Top 
Dim upperBackgroundRectangle = New Rectangle(0, 0, imageSize.Width, pointFontSize * 2) 
graphic.DrawRectangle(Pens.Black, upperBackgroundRectangle) 
graphic.FillRectangle(Brushes.Black, upperBackgroundRectangle) 
graphic.DrawString(upperTitleCommentString, watermarkFont, New SolidBrush(Color.White), New Point(0, 3)) 

'' ... and more stuff ... 

我懷疑我需要改變某種縮放模式進行,或者創建一個新的畫布或東西是比我原來的圖像更大?想法/建議將不勝感激。謝謝。

回答

0

我解決它稍有不同。下面是做工作中的作用:

'' new function which creates a new image which is taller by extraHeight, and pastes the source image into it at pasteHeight 
Private Function addBufferToImage(ByVal source As Image, ByVal backgroundColor As Color, ByVal extraHeight As Integer, ByVal pasteHeight As Integer) As Image 
    Dim newBitmap As New Bitmap(source.Width, source.Height + extraHeight) 
    Dim graphic As Graphics = Graphics.FromImage(newBitmap) 
    graphic.Clear(backgroundColor) 
    graphic.DrawImage(source, 0, pasteHeight) ' draw the original at (0, pasteHeight) on the new image 
    graphic.Dispose() 
    Return newBitmap 
End Function 

然後我調用該函數在我的代碼:

'' create a new Bitmap Object 
Dim exifOverlayImage As New System.Drawing.Bitmap(originalImage, originalImageSize) 
exifOverlayImage = addBufferToImage(exifOverlayImage, Color.Black, extraHeight, pasteHeight) 
'' now I can write text in the new blank areas of the image 
1

您必須考慮新添加的寬度和高度來創建位圖。這是我做了(這是在C#中,但你可以使用www.developerfusion.com/tools/convert/csharp-to-vb/將它轉換):(寬度+ pixelPadding和**高度+ bottomSize + pixelPadding **是其中增加的補白。)

  using (
      var dst = new Bitmap(width + pixelPadding, height + bottomSize + pixelPadding, PixelFormat.Format24bppRgb)) 
     { 
      var rSrcImg = new Rectangle(0,0, src.Width, src.Height); 
      var rDstImg = new Rectangle(pixelPadding/2, pixelPadding/2, dst.Width - pixelPadding, dst.Height - pixelPadding - bottomSize); 
      using (Graphics g = Graphics.FromImage(dst)) 
      { 
       g.Clear(Color.FromArgb(64, 64, 64)); 
       g.FillRectangle(Brushes.White, rDstImg); 

       g.CompositingMode = CompositingMode.SourceOver; 
       g.CompositingQuality = CompositingQuality.GammaCorrected; 
       g.SmoothingMode = SmoothingMode.HighQuality; 
       g.InterpolationMode = InterpolationMode.HighQualityBicubic; 

       g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; 
       g.DrawImage(src, rDstImg, rSrcImg, GraphicsUnit.Pixel); 
      } 

      var ms = new MemoryStream(); 

      // save the bitmap to the stream... 
      dst.Save(ms, ImageFormat.Png); 
      ms.Position = 0; 

我已經在http://www.devzone.ir/post/1389/09/16/Dynamic-Thumbnails-in-ASP.aspx上寫過關於它的博客文章。它調整大小添加一些填充圖像。
基本上,你必須創建一個大小爲圖像+填充的位圖。然後用你的圖像填充一個矩形,並在你想要的區域繪製一個字符串。 該文章是波斯文,但您可以使用谷歌翻譯。我在那裏提供了源代碼。