You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中BC30311错误:‘Rectangle’类型无法转换为‘Rectangle’求助

解决BC30311错误:‘Rectangle’类型无法转换为‘Rectangle’

这个错误的核心原因是命名空间冲突——你的项目中同时存在两个不同命名空间下的Rectangle类型(最常见的是System.Drawing.Rectangle和第三方组件/自定义代码中的Rectangle),编译器无法判断你要使用的具体是哪一个。

解决方案:

  • 明确指定命名空间
    在创建Rectangle对象时,加上完整的命名空间前缀,直接告诉编译器要使用系统绘图类库中的Rectangle:

    DataLockersList.DrawToBitmap(bitmap, New System.Drawing.Rectangle(0, 0, Me.DataLockersList.Width, Me.DataLockersList.Height))
    
  • 排查冲突来源

    1. 检查项目引用列表,确认是否引入了包含自定义Rectangle类型的第三方组件
    2. 查看代码文件顶部的Imports语句,若有导入包含冲突Rectangle的命名空间,可移除或调整导入顺序
    3. 如果项目中自己定义了Rectangle类/结构体,建议重命名该类型,避免和系统内置类型重名

修改后的完整代码示例:

Private bitmap As Bitmap
Private Sub BtnPrintCS_Click(sender As Object, e As EventArgs) Handles BtnPrintCS.Click
    'Resize DataGridView to full height.
    Dim height As Integer = DataLockersList.Height
    DataLockersList.Height = DataLockersList.RowCount * DataLockersList.RowTemplate.Height

    'Create a Bitmap and draw the DataGridView on it.
    bitmap = New Bitmap(Me.DataLockersList.Width, Me.DataLockersList.Height)
    '明确指定System.Drawing命名空间下的Rectangle
    DataLockersList.DrawToBitmap(bitmap, New System.Drawing.Rectangle(0, 0, Me.DataLockersList.Width, Me.DataLockersList.Height))

    'Resize DataGridView back to original height.
    DataLockersList.Height = height

    'Show the Print Preview Dialog.
    PrintPreviewDialog1.Document = PrintDocument1
    PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
    PrintPreviewDialog1.ShowDialog()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    'Print the contents.
    e.Graphics.DrawImage(bitmap, 0, 0)
End Sub

内容的提问来源于stack exchange,提问作者Waqar Vickey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 13:50:39