我正在尝试调整.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 ...
我怀疑我需要更改某种缩放模式,或者创建一个新的画布或比我的原始图像更大的东西?我们将非常感谢您的意见/建议。感谢。
答案 0 :(得分:1)
您必须考虑新添加的宽度和高度来创建位图。这就是我做的方式(它在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写了一篇关于它的博客文章。它调整的大小,为图像添加一些填充 基本上,您必须创建大小图像+填充的位图。然后用图像填充一个矩形,并在所需区域绘制一个字符串。 这篇文章是波斯文,但你可以使用谷歌翻译。我在那里提供了源代码。
答案 1 :(得分: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