Jimi2000

Transparent Bitmaps

Apr 29th, 2018
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 15.93 KB | None | 0 0
  1. '-------------------------------------------------------------------------------
  2. 'Designer Part:
  3. 'Add a new Form to a Project, then click the "components as IContainer" element
  4. 'in Solution Explorer. This opens the Form.Designer. Substitute all the existing
  5. 'code with the First Part of this code.
  6. '-------------------------------------------------------------------------------
  7. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  8. Partial Class PaintForm
  9.     Inherits System.Windows.Forms.Form
  10.  
  11.     <System.Diagnostics.DebuggerNonUserCode()> _
  12.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  13.         Try
  14.             If disposing AndAlso components IsNot Nothing Then
  15.                 components.Dispose()
  16.             End If
  17.         Finally
  18.             MyBase.Dispose(disposing)
  19.         End Try
  20.     End Sub
  21.  
  22.     Private components As System.ComponentModel.IContainer
  23.  
  24.     <System.Diagnostics.DebuggerStepThrough()> _
  25.     Private Sub InitializeComponent()
  26.         Me.TrackBar1 = New System.Windows.Forms.TrackBar()
  27.         Me.TextBox2 = New System.Windows.Forms.TextBox()
  28.         Me.TextBox1 = New System.Windows.Forms.TextBox()
  29.         Me.ComboBox1 = New System.Windows.Forms.ComboBox()
  30.         Me.Label1 = New System.Windows.Forms.Label()
  31.         Me.Label3 = New System.Windows.Forms.Label()
  32.         Me.Panel1 = New System.Windows.Forms.Panel()
  33.         Me.btnOpenFile = New System.Windows.Forms.Button()
  34.         Me.Label4 = New System.Windows.Forms.Label()
  35.         Me.Label5 = New System.Windows.Forms.Label()
  36.         Me.TextBox3 = New System.Windows.Forms.TextBox()
  37.         Me.TextBox4 = New System.Windows.Forms.TextBox()
  38.         Me.TrackBar2 = New System.Windows.Forms.TrackBar()
  39.         Me.btnUp = New System.Windows.Forms.Button()
  40.         CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
  41.         CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).BeginInit()
  42.         Me.SuspendLayout()
  43.         '
  44.         'TrackBar1
  45.         '
  46.         Me.TrackBar1.LargeChange = 200
  47.         Me.TrackBar1.Location = New System.Drawing.Point(36, 86)
  48.         Me.TrackBar1.Maximum = 1000
  49.         Me.TrackBar1.Name = "TrackBar1"
  50.         Me.TrackBar1.Size = New System.Drawing.Size(135, 45)
  51.         Me.TrackBar1.SmallChange = 20
  52.         Me.TrackBar1.TabIndex = 5
  53.         Me.TrackBar1.TabStop = False
  54.         Me.TrackBar1.TickFrequency = 20
  55.         Me.TrackBar1.Value = 300
  56.         '
  57.         'TextBox2
  58.         '
  59.         Me.TextBox2.Location = New System.Drawing.Point(462, 460)
  60.         Me.TextBox2.Name = "TextBox2"
  61.         Me.TextBox2.Size = New System.Drawing.Size(34, 20)
  62.         Me.TextBox2.TabIndex = 4
  63.         Me.TextBox2.Text = "100"
  64.         Me.TextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
  65.         '
  66.         'TextBox1
  67.         '
  68.         Me.TextBox1.Location = New System.Drawing.Point(399, 460)
  69.         Me.TextBox1.Name = "TextBox1"
  70.         Me.TextBox1.Size = New System.Drawing.Size(34, 20)
  71.         Me.TextBox1.TabIndex = 3
  72.         Me.TextBox1.Text = "100"
  73.         Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
  74.         '
  75.         'ComboBox1
  76.         '
  77.         Me.ComboBox1.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  78.         Me.ComboBox1.ForeColor = System.Drawing.Color.Black
  79.         Me.ComboBox1.FormattingEnabled = True
  80.         Me.ComboBox1.Location = New System.Drawing.Point(135, 459)
  81.         Me.ComboBox1.Name = "ComboBox1"
  82.         Me.ComboBox1.Size = New System.Drawing.Size(159, 23)
  83.         Me.ComboBox1.TabIndex = 1
  84.         '
  85.         'Label1
  86.         '
  87.         Me.Label1.AutoSize = True
  88.         Me.Label1.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  89.         Me.Label1.ForeColor = System.Drawing.Color.White
  90.         Me.Label1.Location = New System.Drawing.Point(376, 462)
  91.         Me.Label1.Name = "Label1"
  92.         Me.Label1.Size = New System.Drawing.Size(17, 15)
  93.         Me.Label1.TabIndex = 12
  94.         Me.Label1.Text = "X:"
  95.         '
  96.         'Label3
  97.         '
  98.         Me.Label3.AutoSize = True
  99.         Me.Label3.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  100.         Me.Label3.ForeColor = System.Drawing.Color.White
  101.         Me.Label3.Location = New System.Drawing.Point(439, 462)
  102.         Me.Label3.Name = "Label3"
  103.         Me.Label3.Size = New System.Drawing.Size(17, 15)
  104.         Me.Label3.TabIndex = 13
  105.         Me.Label3.Text = "Y:"
  106.         '
  107.         'Panel1
  108.         '
  109.         Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  110.         Me.Panel1.Location = New System.Drawing.Point(42, 128)
  111.         Me.Panel1.Name = "Panel1"
  112.         Me.Panel1.Size = New System.Drawing.Size(454, 315)
  113.         Me.Panel1.TabIndex = 14
  114.         '
  115.         'btnOpenFile
  116.         '
  117.         Me.btnOpenFile.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer))
  118.         Me.btnOpenFile.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
  119.         Me.btnOpenFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat
  120.         Me.btnOpenFile.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  121.         Me.btnOpenFile.ForeColor = System.Drawing.Color.White
  122.         Me.btnOpenFile.Location = New System.Drawing.Point(42, 456)
  123.         Me.btnOpenFile.Name = "btnOpenFile"
  124.         Me.btnOpenFile.Size = New System.Drawing.Size(74, 27)
  125.         Me.btnOpenFile.TabIndex = 0
  126.         Me.btnOpenFile.Text = "Open new"
  127.         Me.btnOpenFile.UseVisualStyleBackColor = True
  128.         '
  129.         'Label4
  130.         '
  131.         Me.Label4.AutoSize = True
  132.         Me.Label4.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  133.         Me.Label4.ForeColor = System.Drawing.Color.White
  134.         Me.Label4.Location = New System.Drawing.Point(277, 95)
  135.         Me.Label4.Name = "Label4"
  136.         Me.Label4.Size = New System.Drawing.Size(42, 15)
  137.         Me.Label4.TabIndex = 19
  138.         Me.Label4.Text = "Width:"
  139.         '
  140.         'Label5
  141.         '
  142.         Me.Label5.AutoSize = True
  143.         Me.Label5.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  144.         Me.Label5.ForeColor = System.Drawing.Color.White
  145.         Me.Label5.Location = New System.Drawing.Point(176, 95)
  146.         Me.Label5.Name = "Label5"
  147.         Me.Label5.Size = New System.Drawing.Size(46, 15)
  148.         Me.Label5.TabIndex = 18
  149.         Me.Label5.Text = "Height:"
  150.         '
  151.         'TextBox3
  152.         '
  153.         Me.TextBox3.Location = New System.Drawing.Point(226, 93)
  154.         Me.TextBox3.Name = "TextBox3"
  155.         Me.TextBox3.Size = New System.Drawing.Size(34, 20)
  156.         Me.TextBox3.TabIndex = 6
  157.         Me.TextBox3.Text = "100"
  158.         Me.TextBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
  159.         '
  160.         'TextBox4
  161.         '
  162.         Me.TextBox4.Location = New System.Drawing.Point(322, 93)
  163.         Me.TextBox4.Name = "TextBox4"
  164.         Me.TextBox4.Size = New System.Drawing.Size(34, 20)
  165.         Me.TextBox4.TabIndex = 7
  166.         Me.TextBox4.Text = "100"
  167.         Me.TextBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
  168.         '
  169.         'TrackBar2
  170.         '
  171.         Me.TrackBar2.LargeChange = 200
  172.         Me.TrackBar2.Location = New System.Drawing.Point(367, 86)
  173.         Me.TrackBar2.Maximum = 1000
  174.         Me.TrackBar2.Name = "TrackBar2"
  175.         Me.TrackBar2.Size = New System.Drawing.Size(135, 45)
  176.         Me.TrackBar2.SmallChange = 20
  177.         Me.TrackBar2.TabIndex = 8
  178.         Me.TrackBar2.TabStop = False
  179.         Me.TrackBar2.TickFrequency = 20
  180.         Me.TrackBar2.Value = 300
  181.         '
  182.         'btnUp
  183.         '
  184.         Me.btnUp.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer))
  185.         Me.btnUp.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
  186.         Me.btnUp.FlatStyle = System.Windows.Forms.FlatStyle.Flat
  187.         Me.btnUp.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  188.         Me.btnUp.ForeColor = System.Drawing.Color.White
  189.         Me.btnUp.Location = New System.Drawing.Point(300, 458)
  190.         Me.btnUp.Name = "btnUp"
  191.         Me.btnUp.Size = New System.Drawing.Size(32, 25)
  192.         Me.btnUp.TabIndex = 2
  193.         Me.btnUp.Text = "Up"
  194.         Me.btnUp.UseVisualStyleBackColor = True
  195.         '
  196.         'PaintForm
  197.         '
  198.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
  199.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  200.         Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer))
  201.         Me.ClientSize = New System.Drawing.Size(522, 463)
  202.         Me.Controls.Add(Me.btnUp)
  203.         Me.Controls.Add(Me.Panel1)
  204.         Me.Controls.Add(Me.TrackBar2)
  205.         Me.Controls.Add(Me.Label4)
  206.         Me.Controls.Add(Me.Label5)
  207.         Me.Controls.Add(Me.TextBox3)
  208.         Me.Controls.Add(Me.TextBox4)
  209.         Me.Controls.Add(Me.btnOpenFile)
  210.         Me.Controls.Add(Me.Label3)
  211.         Me.Controls.Add(Me.Label1)
  212.         Me.Controls.Add(Me.ComboBox1)
  213.         Me.Controls.Add(Me.TextBox1)
  214.         Me.Controls.Add(Me.TextBox2)
  215.         Me.Controls.Add(Me.TrackBar1)
  216.         Me.DoubleBuffered = True
  217.         Me.Name = "PaintForm"
  218.         Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  219.         Me.Text = "PaintForm"
  220.         CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
  221.         CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).EndInit()
  222.         Me.ResumeLayout(False)
  223.         Me.PerformLayout()
  224.  
  225.     End Sub
  226.  
  227.     Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar
  228.     Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
  229.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  230.     Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
  231.     Friend WithEvents Label1 As System.Windows.Forms.Label
  232.     Friend WithEvents Label3 As System.Windows.Forms.Label
  233.     Friend WithEvents Panel1 As System.Windows.Forms.Panel
  234.     Friend WithEvents btnOpenFile As System.Windows.Forms.Button
  235.     Friend WithEvents Label4 As System.Windows.Forms.Label
  236.     Friend WithEvents Label5 As System.Windows.Forms.Label
  237.     Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
  238.     Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
  239.     Friend WithEvents TrackBar2 As System.Windows.Forms.TrackBar
  240.     Friend WithEvents btnUp As System.Windows.Forms.Button
  241. End Class
  242.  
  243. '-------------------------------------------------------------------------------
  244. 'Designer Part: End of file
  245. '-------------------------------------------------------------------------------
  246.  
  247. '-------------------------------------------------------------------------------
  248. 'Code Part:
  249. 'Insert the following code in the Code Section of the new Form.
  250. 'Substitute all of the pre-defined code with this.
  251. '-------------------------------------------------------------------------------
  252.  
  253. Imports System.Drawing
  254. Imports System.Drawing.Drawing2D
  255. Imports System.Drawing.Imaging
  256. Imports System.IO
  257.  
  258. Public Class PaintForm
  259.  
  260.     Public Class BitmapObject
  261.         Public Property Name As String
  262.         Public Property Image As Bitmap
  263.         Public Property Position As Point
  264.         Public Property Size As Size
  265.         Public Property Order As Integer
  266.     End Class
  267.  
  268.     Public MyBitmaps As List(Of BitmapObject) = New List(Of BitmapObject)
  269.  
  270.     Private Sub btnOpenFile_Click(sender As Object, e As EventArgs) Handles btnOpenFile.Click
  271.         Dim fd As OpenFileDialog = New OpenFileDialog()
  272.         fd.InitialDirectory = "d:\images\"
  273.         Dim dr As DialogResult = fd.ShowDialog()
  274.         If dr = Windows.Forms.DialogResult.OK Then
  275.             Dim BitmapName As String = New FileInfo(fd.FileName).Name
  276.             Using tmpBitmap As Bitmap = New Bitmap(fd.FileName)
  277.                 MyBitmaps.Add(New BitmapObject With {
  278.                               .Image = New Bitmap(tmpBitmap),
  279.                               .Position = New Point(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text)),
  280.                               .Size = New Size(tmpBitmap.Height, tmpBitmap.Width),
  281.                               .Order = MyBitmaps.Count,
  282.                               .Name = BitmapName})
  283.                 ComboBox1.Items.Add(BitmapName)
  284.                 ComboBox1.SelectedIndex = MyBitmaps.Count - 1
  285.                 TrackBar1.Value = tmpBitmap.Height
  286.                 TrackBar2.Value = tmpBitmap.Width
  287.                 Panel1.Invalidate()
  288.             End Using
  289.         End If
  290.     End Sub
  291.  
  292.  
  293.     Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
  294.         If MyBitmaps.Count > 0 Then
  295.             MyBitmaps.OrderBy(Function(item) item.Order).
  296.                 Select(Function(item)
  297.                            e.Graphics.DrawImage(item.Image, New Rectangle(item.Position, item.Size))
  298.                            Return item
  299.                        End Function).ToList()
  300.         End If
  301.     End Sub
  302.  
  303.     Private Sub TBoxPosition_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown
  304.         If (e.KeyValue = Keys.Enter) AndAlso (MyBitmaps.Count > 0) Then
  305.             MyBitmaps(ComboBox1.SelectedIndex).Position = New Point(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text))
  306.             Panel1.Invalidate()
  307.         End If
  308.     End Sub
  309.  
  310.     Private Sub TBox_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter, TextBox4.Enter
  311.         CType(sender, TextBox).SelectAll()
  312.     End Sub
  313.  
  314.     Private Sub TBar_ChangeSize(sender As Object, e As EventArgs) Handles TrackBar1.Scroll, TrackBar2.Scroll
  315.         TextBox3.Text = TrackBar1.Value.ToString
  316.         TextBox4.Text = TrackBar2.Value.ToString
  317.         MyBitmaps(ComboBox1.SelectedIndex).Size = New Size(TrackBar1.Value, TrackBar2.Value)
  318.         Panel1.Invalidate()
  319.     End Sub
  320.  
  321.     Private Sub TBar_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged, TrackBar2.ValueChanged
  322.         TextBox3.Text = TrackBar1.Value.ToString
  323.         TextBox4.Text = TrackBar2.Value.ToString
  324.     End Sub
  325.  
  326.     Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
  327.         TrackBar1.Value = MyBitmaps(ComboBox1.SelectedIndex).Size.Height
  328.         TrackBar2.Value = MyBitmaps(ComboBox1.SelectedIndex).Size.Width
  329.         TextBox1.Text = MyBitmaps(ComboBox1.SelectedIndex).Position.X.ToString
  330.         TextBox2.Text = MyBitmaps(ComboBox1.SelectedIndex).Position.Y.ToString
  331.     End Sub
  332.  
  333.     Private Sub btnUp_Click_1(sender As Object, e As EventArgs) Handles btnUp.Click
  334.         Dim CurrentImageOrder As Integer = MyBitmaps(ComboBox1.SelectedIndex).Order
  335.         For Each Item As BitmapObject In MyBitmaps
  336.             If Item.Order = MyBitmaps.Count - 1 Then
  337.                 Item.Order = CurrentImageOrder
  338.                 Exit For
  339.             End If
  340.         Next
  341.         MyBitmaps(ComboBox1.SelectedIndex).Order = MyBitmaps.Count - 1
  342.         Panel1.Invalidate()
  343.     End Sub
  344.  
  345. End Class
  346.  
  347. '-------------------------------------------------------------------------------
  348. 'Code Part: End of file.
  349. '-------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment