通过VB.NET脚本向图像添加填充 [英] Adding padding to an image via VB.NET script

查看:105
本文介绍了通过VB.NET脚本向图像添加填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在将图片添加到标签控制标签页,我已设法将其设置为罚款(此图片稍后将成为关闭按钮)。我的问题是如何在这个图像上添加一些填充,以便它在实际标签上看起来更好?我的代码如下:



Hi everyone,

I'm adding an image to a tab control tabpage, which I have managed to to fine (this image will later be a close button). My question is how can I put some padding onto this image so that it looks better on the actual tab? My code is below:

Private Sub tabControl_drawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Try
            Dim img As Image = New Bitmap("C:\close.png")
            Dim r As Rectangle = e.Bounds
            r = Me.TabControl.GetTabRect(e.Index)
            r.Offset(2, 2)
            Dim TitleBrush As Brush = New SolidBrush(Color.Black)
            Dim f As Font = Me.Font
            Dim Title As String = Me.TabControl.TabPages(e.Index).Text
            e.Graphics.DrawString(Title, f, TitleBrush, New PointF(r.X, r.Y))
            e.Graphics.DrawImage(img, New Point(r.X + (Me.TabControl.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y))
            img..Padding = New System.Drawing.Point(10, 5)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

推荐答案

没有填充这样的东西 System.Drawing.Graphics ,所以只需添加一些移动到您已经使用的图像的坐标。为了保持一致性,这种转换可能是使用自定义控件类控件的表单的成员。是的,就这么简单。



-SA
There is no such thing as "padding" for System.Drawing.Graphics, so just add some shift to the coordinates of the image you already use. For consistency, this shift could be a member of the form using the control of a custom control class, perhaps. Yes, as simple as that.

—SA


尽管谢尔盖说的是,它是可能。我正在创建一个.gif文件,并希望我的所有图像都填充以保持恒定的宽度和高度。在这里,我查看我的图像数组,进入.gif并找到最大高度和宽度。



然后我将画布设置为最大高度和宽度,并将所有像素的颜色设置为白色(这可能是一种更有效的方法,而不是像素一样按像素)。如果要获取像素颜色,只需使用GetPixel选项即可。这些只适用于位图 - 而不是图像。



然后我得到我的x和y偏移,这样我就可以将图像放在画布的中心然后保存到我的aryImages(i)._ Image_Resized变量或者你可以直接保存到文件。



这可能是几年来太晚无法帮助你但希望能帮助其他人有相同问题的人。



Despite what Sergey says, it is possible. I'm creating a .gif and wanted all my images padded to maintain a constant width and height. Here I look through my array of images to go into the .gif and find the maximum height and width.

I then set my canvas to be the maximum height and width and colour all it's pixels white (there may well be a more efficient way of doing this rather than going pixel by pixel). If you want to get the pixel colour, you just use the GetPixel option. These are only available with bitmaps - not images.

I then get my x and y offsets so I can position the image in the centre of the canvas and then save this to my aryImages(i)._Image_Resized variable or you can save straight to file.

This may be a couple of years too late to help you but hopefully will help other people with the same issue.

Dim MaxWidth As Integer = 0
Dim MaxHeight As Integer = 0

For i = 1 To aryImageCount
    If aryImages(i)._Image.Width > MaxWidth Then MaxWidth = aryImages(i)._Image.Width
    If aryImages(i)._Image.Height > MaxHeight Then MaxHeight = aryImages(i)._Image.Height
Next

Dim canvasWidth As Integer = MaxWidth
Dim canvasHeight As Integer = MaxHeight
Dim x_offset As Integer
Dim y_offset As Integer

For i = 1 To aryImageCount

    Dim Canvas As Bitmap = New Bitmap(canvasWidth, canvasHeight)
    Dim gfx As Graphics = Graphics.FromImage(Canvas)

    'Process the images pixels
    For y As Integer = 0 To canvasHeight - 1
        For x As Integer = 0 To canvasWidth - 1
            'Set this pixel's color
            Canvas.SetPixel(x, y, Color.White)
        Next
    Next

    x_offset = (MaxWidth - aryImages(i)._Image.Width) / 2
    y_offset = (MaxHeight - aryImages(i)._Image.Height) / 2

    gfx.DrawImage(aryImages(i)._Image, x_offset, y_offset, aryImages(i)._Image.Width, aryImages(i)._Image.Height)



    aryImages(i)._Image_Resized = New Bitmap(Canvas)


这篇关于通过VB.NET脚本向图像添加填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆