【问题标题】:Adding text BELOW bitmap in VB.NET在 VB.NET 中添加位图下方的文本
【发布时间】:2011-07-24 23:08:38
【问题描述】:

我试图在不实际缩放图像的情况下调整 .NET 位图的大小。这里的想法是在图像上方和下方创建一个空间,用黑色矩形填充它,并在其中放置一些文本(不覆盖或破坏原始图像的任何部分)。

到目前为止,我看到的所有代码和示例都只是展示了如何缩放图像,而不是扩展画布。到目前为止,我自己尝试过的所有操作都会缩放/拉伸图像。

这是我所拥有的:

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 ...

我怀疑我需要更改某种缩放模式,或者创建一个新画布或比我的原始图像更大的东西?想法/建议将不胜感激。谢谢。

【问题讨论】:

    标签: .net vb.net image image-processing


    【解决方案1】:

    我的解决方法略有不同。这是完成这项工作的函数:

    '' 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
    

    【讨论】:

      【解决方案2】:

      您必须在考虑新添加的宽度和高度的情况下创建位图。我就是这样做的(它在 C# 中,但您可以使用 www.developerfusion.com/tools/convert/csharp-to-vb/ 来转换它):(width + pixelPadding 和 ** height + 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 写了一篇关于它的博客文章。它会调整大小并为图像添加一些填充。
      基本上,您必须创建一个大小为图像 + 填充的位图。然后用您的图像填充一个矩形并在您想要的区域绘制一个字符串。 这篇文章是波斯语的,但你可以使用谷歌翻译。我已经在那里提供了源代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多