无法将类型为“System.Drawing.Bitmap”的对象强制转换为类型“System.ComponentModel.IContainer”。

BulletShi 2017-02-14 08:15:49
Private _docement As DocementBase
'打印
Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
If pictureBox1.Image Is Nothing Then
MessageBox.Show("You Must Load an Image first!")
Return
Else
_docement = New imageDocument(pictureBox1.Image)
End If
_docement.showPrintPreviewDialog()
End Sub


_docement = New imageDocument(pictureBox1.Image)这行报错, '无法将类型为“System.Drawing.Bitmap”的对象强制转换为类型“System.ComponentModel.IContainer”。

其中imageDocument在组件类中有定义
Public Class imageDocument : Inherits DocementBase

Private _Image As Image

Public Property Image() As Image

Get
Return _Image
End Get

Set(value As Image)
_Image = value
If _Image IsNot Nothing Then
If _Image.Size.Width > _Image.Size.Height Then

DefaultPageSettings.Landscape = True
Else
DefaultPageSettings.Landscape = False
End If
End If
End Set
End Property

Public Sub imageDocument()
End Sub

Public Sub imageDocument(image As Image)
Me.Image = image
End Sub
...全文
485 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BulletShi 2017-02-14
  • 打赏
  • 举报
回复
谢谢 Tiger_Zhao ,已好!
BulletShi 2017-02-14
  • 打赏
  • 举报
回复
谢谢 xdashewan ,一语中的,疏忽了。非常感谢!
Tiger_Zhao 2017-02-14
  • 打赏
  • 举报
回复
VB.Net 中的构造函数的命名 New 而不是类名
Public Class imageDocument : Inherits DocementBase

Public Sub New(image As Image)
Me.Image = image
End Sub

End Class
xdashewan 2017-02-14
  • 打赏
  • 举报
回复
vb.net中的构造函数为Public Sub New函数,而不是Public Sub 类名
BulletShi 2017-02-14
  • 打赏
  • 举报
回复
在C#中可以正常使用,转为VB为何查询引用时指定会不同
BulletShi 2017-02-14
  • 打赏
  • 举报
回复
imageDocument在查询引用时查到的是类Public Class imageDocument : Inherits DocementBase ,而不是类中定义的 Public Sub imageDocument(image As Image) Me.Image = image End Sub
Option Strict OnImports System.MathPublic Class MandelbrotForm Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code " Public Sub New() MyBase.New() ‘This call is required by the Windows Form Designer. InitializeComponent() ‘Add any initialization after the InitializeComponent() call End Sub ‘Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub ‘Required by the Windows Form Designer Private components As System.ComponentModel.IContainer ‘NOTE: The following procedure is required by the Windows Form Designer ‘It can be modified using the Windows Form Designer. ‘Do not modify it using the code editor. Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents txtMin As System.Windows.Forms.TextBox Friend WithEvents txtMax As System.Windows.Forms.TextBox Friend WithEvents txtColorMin As System.Windows.Forms.TextBox Friend WithEvents txtColorMax As System.Windows.Forms.TextBox Friend WithEvents bttnMandelbrot As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents bttnViewPrevious As System.Windows.Forms.Button Friend WithEvents bttnViewNext As System.Windows.Forms.Button Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MandelbrotForm)) Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.bttnMandelbrot = New System.Windows.Forms.Button Me.bttnViewPrevious = New System.Windows.Forms.Button Me.bttnViewNext = New System.Windows.Forms.Button Me.txtMin = New System.Windows.Forms.TextBox Me.txtMax = New System.Windows.Forms.TextBox Me.txtColorMin = New System.Windows.Forms.TextBox Me.txtColorMax = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.Label6 = New System.Windows.Forms.Label Me.SuspendLayout() ‘ ‘PictureBox1 ‘ Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.PictureBox1.Location = New System.Drawing.Point(5, 97) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(512, 512) Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False ‘ ‘bttnMandelbrot ‘ Me.bttnMandelbrot.Font = New System.Drawing.Font("Verdana", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnMandelbrot.Location = New System.Drawing.Point(62, 4) Me.bttnMandelbrot.Name = "bttnMandelbrot" Me.bttnMandelbrot.Size = New System.Drawing.Size(399, 30) Me.bttnMandelbrot.TabIndex = 1 Me.bttnMandelbrot.Text = "New Fractal" ‘ ‘bttnViewPrevious ‘ Me.bttnViewPrevious.Image = CType(resources.GetObject("bttnViewPrevious.Image"), System.Drawing.Image) Me.bttnViewPrevious.Location = New System.Drawing.Point(3, 4) Me.bttnViewPrevious.Name = "bttnViewPrevious" Me.bttnViewPrevious.Size = New System.Drawing.Size(50, 30) Me.bttnViewPrevious.TabIndex = 3 ‘ ‘bttnViewNext ‘ Me.bttnViewNext.Image = CType(resources.GetObject("bttnViewNext.Image"), System.Drawing.Image) Me.bttnViewNext.Location = New System.Drawing.Point(470, 4) Me.bttnViewNext.Name = "bttnViewNext" Me.bttnViewNext.Size = New System.Drawing.Size(50, 30) Me.bttnViewNext.TabIndex = 4 ‘ ‘txtMin ‘ Me.txtMin.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtMin.Location = New System.Drawing.Point(61, 67) Me.txtMin.Name = "txtMin" Me.txtMin.Size = New System.Drawing.Size(46, 26) Me.txtMin.TabIndex = 6 Me.txtMin.Text = "0" ‘ ‘txtMax ‘ Me.txtMax.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtMax.Location = New System.Drawing.Point(165, 67) Me.txtMax.Name = "txtMax" Me.txtMax.Size = New System.Drawing.Size(46, 26) Me.txtMax.TabIndex = 7 Me.txtMax.Text = "128" ‘ ‘txtColorMin ‘ Me.txtColorMin.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtColorMin.Location = New System.Drawing.Point(336, 67) Me.txtColorMin.Name = "txtColorMin" Me.txtColorMin.Size = New System.Drawing.Size(46, 26) Me.txtColorMin.TabIndex = 9 Me.txtColorMin.Text = "0" ‘ ‘txtColorMax ‘ Me.txtColorMax.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtColorMax.Location = New System.Drawing.Point(451, 67) Me.txtColorMax.Name = "txtColorMax" Me.txtColorMax.Size = New System.Drawing.Size(46, 26) Me.txtColorMax.TabIndex = 10 Me.txtColorMax.Text = "512" ‘ ‘Label1 ‘ Me.Label1.Font = New System.Drawing.Font("Verdana", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label1.Location = New System.Drawing.Point(11, 41) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(121, 22) Me.Label1.TabIndex = 11 Me.Label1.Text = "Iterations" ‘ ‘Label2 ‘ Me.Label2.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label2.Location = New System.Drawing.Point(16, 72) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(39, 16) Me.Label2.TabIndex = 12 Me.Label2.Text = "Min" ‘ ‘Label3 ‘ Me.Label3.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label3.Location = New System.Drawing.Point(120, 72) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(46, 16) Me.Label3.TabIndex = 13 Me.Label3.Text = "Max" ‘ ‘Label4 ‘ Me.Label4.Font = New System.Drawing.Font("Verdana", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label4.Location = New System.Drawing.Point(285, 41) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(158, 22) Me.Label4.TabIndex = 14 Me.Label4.Text = "Color Range" ‘ ‘Label5 ‘ Me.Label5.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label5.Location = New System.Drawing.Point(293, 72) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(39, 16) Me.Label5.TabIndex = 15 Me.Label5.Text = "Min" ‘ ‘Label6 ‘ Me.Label6.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Label6.Location = New System.Drawing.Point(402, 72) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(46, 16) Me.Label6.TabIndex = 16 Me.Label6.Text = "Max" ‘ ‘MandelbrotForm ‘ Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(524, 620) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtColorMax) Me.Controls.Add(Me.txtColorMin) Me.Controls.Add(Me.txtMax) Me.Controls.Add(Me.txtMin) Me.Controls.Add(Me.bttnViewNext) Me.Controls.Add(Me.bttnViewPrevious) Me.Controls.Add(Me.bttnMandelbrot) Me.Controls.Add(Me.PictureBox1) Me.KeyPreview = True Me.Name = "MandelbrotForm" Me.Text = "Fractal Generator: The Mandelbrot Set" Me.ResumeLayout(False) End Sub#End Region Dim XMin As Double, XMax As Double Dim YMin As Double, YMax As Double Dim XStart, YStart As Integer Dim XEnd, YEnd As Integer Dim currentFractal As Integer Dim breaknow As Boolean Dim limits As New ArrayList Structure Bounding Dim XMin As Double Dim XMax As Double Dim YMin As Double Dim YMax As Double End Structure Private Sub bttnMandelbrot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMandelbrot.Click Static IterMin As Integer = 100000 Dim IterMax As Integer = -100000 Dim bmap As Bitmap bmap = New Bitmap(512, 512, Drawing.Imaging.PixelFormat.Format32bppPArgb) PictureBox1.Image = bmap Dim pX, pY As Integer Dim NX As Integer = 512 Dim NY As Integer = 512 Dim MaxIter As Integer Dim X, Y As Double Dim Iterations As Integer Dim minIterations As Integer = CInt(txtMin.Text) Dim maxIterations As Integer = CInt(txtMax.Text) Dim colorMin As Integer Dim colorMax As Integer Dim pixelColor As Color If IsNumeric(txtColorMin.Text) Then colorMin = CInt(txtColorMin.Text) If IsNumeric(txtColorMax.Text) Then colorMax = CInt(txtColorMax.Text) If IsNumeric(txtMin.Text) Then minIterations = CInt(txtMin.Text) If IsNumeric(txtMax.Text) Then maxIterations = CInt(txtMax.Text) If maxIterations <= minIterations Then MsgBox("The number of maximum iterations should be larger " & _ "than the number of minimum iterations") Exit Sub End If If colorMax <= colorMin Then MsgBox("The first color‘s value should be smalled than the last color‘s value") Exit Sub End If For pY = 0 To NY - 1 Y = YMin + pY * (YMax - YMin) / (NY - 1) For pX = 0 To NX - 1 X = (XMin + pX * (XMax - XMin) / (NY - 1)) Iterations = Mandelbrot(X, Y, maxIterations) Dim clr As Integer clr = CInt(colorMin + _ (colorMax - colorMin) / (maxIterations - minIterations) * _ (Iterations - minIterations)) clr = Math.Max(minIterations, clr) pixelColor = PaintPixel(clr) bmap.SetPixel(pX, pY, pixelColor) Next PictureBox1.Invalidate() Application.DoEvents() If breaknow Then breaknow = False Exit Sub End If Next End Sub Private Function PaintPixel(ByVal clr As Integer) As Color Select Case clr Case 0 To 256 - 1 Return Color.FromArgb(clr, 0, 0) Case 256 To 256 * 2 - 1 Return Color.FromArgb(255, clr - 256, 0) Case 256 * 2 To 256 * 3 - 1 Return Color.FromArgb(255, 255, clr - 256 * 2) Case 256 * 3 To 256 * 4 - 1 Return Color.FromArgb(255, clr - 256 * 3, clr - 256 * 3) Case 256 * 4 To 256 * 5 - 1 Return Color.FromArgb(255, 256 * 5 - clr - 1, 255) Case 256 * 5 To 256 * 6 - 1 Return Color.FromArgb(clr - 256 * 5, clr - 256 * 5, clr - 256 * 5) Case Else Return Color.FromArgb(clr Mod 256, clr Mod 256, clr Mod 256) End Select End Function Function Mandelbrot(ByVal Cx As Double, ByVal Cy As Double, ByVal max As Integer) As Integer Dim iter As Integer Dim X2, Y2 As Double Dim X, Y As Double Dim temp As Double Dim currentBounds As Bounding currentBounds = CType(limits.Item(currentFractal), Bounding) XMin = currentBounds.XMin XMax = currentBounds.XMax YMin = currentBounds.YMin YMax = currentBounds.YMax While iter < max And (Sqrt(X2 * X2 + Y2 * Y2) < 100000) temp = X2 - Y2 + Cx Y = 2 * X * Y + Cy X = temp X2 = X * X Y2 = Y * Y * Y iter = iter + 1 End While Return (iter) End Function Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown XStart = e.X YStart = e.Y End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If e.Button <> MouseButtons.Left Then Exit Sub PictureBox1.Refresh() Dim newX, newY As Integer newX = e.X newY = e.Y If Math.Abs(newX - XStart) < Math.Abs(newY - YStart) Then newY = YStart + Math.Abs(newX - XStart) Else newX = XStart + Math.Abs(newY - YStart) End If PictureBox1.CreateGraphics.DrawRectangle(Pens.White, XStart, YStart, newX - XStart, newY - YStart) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load XMin = -1.5 : XMax = 0.5 YMin = -1 : YMax = 1 Dim currentBounds As Bounding currentBounds.XMin = XMin currentBounds.XMax = XMax currentBounds.YMin = YMin currentBounds.YMax = YMax limits.Add(currentBounds) End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp XEnd = e.X YEnd = e.Y If Math.Abs(XEnd - XStart) < Math.Abs(YEnd - YStart) Then YEnd = YStart + Math.Abs(XEnd - XStart) Else XEnd = XStart + Math.Abs(YEnd - YStart) End If Dim currentBounds As Bounding currentBounds = CType(limits.Item(currentFractal), Bounding) XMin = currentBounds.XMin XMax = currentBounds.XMax YMin = currentBounds.YMin YMax = currentBounds.YMax If XEnd <> 0 And YEnd <> 0 Then Dim DX, DY As Double DX = XMax - XMin DY = YMax - YMin Dim newXMin, newXMax, newYMin, newYMax As Double newXMin = XMin + DX * XStart / PictureBox1.Width newXMax = XMin + DX * XEnd / PictureBox1.Width newYMin = YMin + DY * YStart / PictureBox1.Height newYMax = YMin + DY * YEnd / PictureBox1.Height XMin = newXMin : XMax = newXMax YMin = newYMin : YMax = newYMax currentBounds.XMin = XMin currentBounds.XMax = XMax currentBounds.YMin = YMin currentBounds.YMax = YMax limits.Add(currentBounds) currentFractal = currentFractal + 1 End If End Sub Private Sub bttnViewNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnViewNext.Click Dim currentBounds As Bounding currentBounds.XMin = XMin currentBounds.XMax = XMax currentBounds.YMin = YMin currentBounds.YMax = YMax limits.Add(currentBounds) currentFractal += 1 If currentFractal <= limits.Count Then bttnMandelbrot_Click(sender, e) End If End Sub Private Sub bttnViewPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnViewPrevious.Click currentFractal = currentFractal - 1 If currentFractal >= 0 Then bttnMandelbrot_Click(sender, e) Else currentFractal = 0 End If End Sub Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Escape Then breaknow = True End If End SubEnd Class

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧