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

VB.NET中InkPicture存入Access数据库遇问题,求技术解决

Fixing InkPicture to Access Database Storage Issues

Hey Ricardo, let's tackle this problem step by step. The core issue here is that you're trying to pass a Bitmap object directly to your TableAdapter, which Access doesn't handle natively—plus, the uncompressed GIF you're generating is likely causing that "binary too long" error. Let's fix both the data conversion and optimize the process to avoid those headaches.

Key Issues in Your Current Code

  • Direct Bitmap Passing: Access OLEDB can't serialize a Bitmap object directly into a binary field. You need to convert the image to a byte array first.
  • Uncompressed GIF: Saving as an uncompressed GIF creates unnecessarily large binary data, which triggers the "binary too long" error. Switching to PNG (lossless compression) will drastically reduce the file size.
  • Resource Leaks: You're not disposing of Bitmap, Graphics, and MemoryStream objects properly, which can cause memory issues over time.
  • Redundant Temp File: You don't need to save the image to a temp file—you can process it entirely in memory to avoid IO overhead.

Fixed Code with Explanations

' Use Using statements to auto-dispose resources and prevent leaks
If InkPicture1.Ink.Strokes.Count > 0 Then
    Dim finalImage As Bitmap = Nothing
    Try
        ' 1. Render ink to a bitmap
        Dim inkBytes() As Byte = InkPicture1.Ink.Save(PersistenceFormat.Gif, CompressionMode.NoCompression)
        Using msInk As New MemoryStream(inkBytes)
            Using inkGif As New Bitmap(msInk)
                ' 2. Create a copy of the background image to draw on
                finalImage = New Bitmap(InkPicture1.BackgroundImage)
                Using g As Graphics = Graphics.FromImage(finalImage)
                    ' Get correct ink position (fix coordinate conversion)
                    Dim inkBox As Rectangle = InkPicture1.Ink.GetBoundingBox()
                    Dim inkLocation As New Point(inkBox.Left, inkBox.Top)
                    Using tempGraphics As Graphics = CreateGraphics()
                        Dim inkRenderer As New Microsoft.Ink.Renderer()
                        inkRenderer.InkSpaceToPixel(tempGraphics, inkLocation)
                    End Using ' Auto-dispose tempGraphics
                    
                    ' Draw the ink onto the background image
                    g.DrawImage(inkGif, New Rectangle(inkLocation.X, inkLocation.Y, inkGif.Width, inkGif.Height))
                End Using ' Auto-dispose Graphics
            End Using ' Auto-dispose inkGif
        End Using ' Auto-dispose msInk

        ' 3. Convert final image to byte array (PNG for smaller size)
        Dim imageBytes() As Byte = Nothing
        Using msFinal As New MemoryStream()
            finalImage.Save(msFinal, System.Drawing.Imaging.ImageFormat.Png)
            imageBytes = msFinal.ToArray()
        End Using

        ' 4. Pass the byte array to your TableAdapter instead of the Bitmap
        Canhotos_AkfixTableAdapter.Gravar_mastik_canhoto(
            doc, 
            serie_doc.Text, 
            n_doc.Text, 
            data.Text, 
            n_cliente.Text, 
            nome_cliente.Text, 
            imageBytes, ' Use byte array here
            p_nome.Text, 
            u_nome.Text, 
            modo_pagamento, 
            dias.Text, 
            total.Text
        )
    Finally
        ' Ensure the finalImage is disposed even if an error occurs
        finalImage?.Dispose()
    End Try
End If

Additional Checks to Avoid Errors

  • Access Field Type: Make sure the field in your Access table is set to OLE Object (for older Access versions) or Attachment (for Access 2007+). If using Attachment, you might need to adjust your TableAdapter code to handle the attachment type properly.
  • Field Size: For OLE Object fields, Access allows up to 2GB of data, so the compressed PNG should fit easily—but double-check that your field isn't set to a smaller size by mistake.
  • TableAdapter Method: Verify that the parameter for the image in your Gravar_mastik_canhoto method is defined as a Byte() (byte array) instead of a Bitmap. If it's currently expecting a Bitmap, you'll need to update the TableAdapter's query to accept a binary parameter.

内容的提问来源于stack exchange,提问作者Ricardo Gonçalves

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:31:25