Wenn ich eine Bitmap an ein Property eines UserControl übergeben will kommt es zum Fehler

Klaus Ruttkowski 0 Reputation points
2024-09-17T11:31:06.94+00:00

Wenn ich in einer Anwendung eine Bitmap an mein eingebundenes UserControl übergeben möchte kommt es seit ich das UserControl unter NET 8.0 erstelle, zu folgendem Fehler

Fehlermeldung Unter NET 4.0 funktionierte das Programm noch.

Warum? Danke für die Hilfe, Gruß Klaus

Hier einige Code-Ausschnitte

[CategoryAttribute("RuSplash Settings")] 
[DescriptionAttribute("Company image")] 
[Browsable(true)] 
public Bitmap CompanyBitmap { set; get; } = (Bitmap)((new               imageConverter()).ConvertFrom(global::RuSplash.CompanyBitmap.KR_200x200));
            // 
            // pictureBoxCompany
            // 
            pictureBoxCompany.Dock = DockStyle.Fill;
            pictureBoxCompany.Location = new Point(0, 0);
            pictureBoxCompany.MaximumSize = new Size(300, 300);
            pictureBoxCompany.Name = "pictureBoxCompany";
            pictureBoxCompany.Size = new Size(105, 105);
            pictureBoxCompany.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBoxCompany.TabIndex = 0;
            pictureBoxCompany.TabStop = false;
            pictureBoxCompany.Image = CompanyBitmap;
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,888 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 31,166 Reputation points Microsoft Vendor
    2024-09-18T01:41:28.2333333+00:00

    Hi @Klaus Ruttkowski ,

    The .NET 8.0 runtime is not able to serialize the System.Drawing.Bitmap object the same way it did in .NET 4.0. In .NET Core and later versions, the System.Drawing.Common library behaves differently compared to .NET Framework, and Bitmap serialization might not be supported directly.

    Instead of trying to directly pass a Bitmap object to the property, use the ImageConverter to convert the Bitmap into a format that can be serialized, such as a byte array or Base64 string.

    [CategoryAttribute("RuSplash Settings")] 
    [DescriptionAttribute("Company image")] 
    [Browsable(true)] 
    public byte[] CompanyBitmap { 
        get {
            if (_companyBitmap != null)
            {
                using (var ms = new MemoryStream())
                {
                    _companyBitmap.Save(ms, ImageFormat.Png);
                    return ms.ToArray();
                }
            }
            return null;
        }
        set {
            if (value != null)
            {
                using (var ms = new MemoryStream(value))
                {
                    _companyBitmap = new Bitmap(ms);
                }
            }
            else
            {
                _companyBitmap = null;
            }
            pictureBoxCompany.Image = _companyBitmap;
        }
    }
    private Bitmap _companyBitmap;
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Klaus Ruttkowski 0 Reputation points
    2024-09-20T12:11:13.33+00:00

    I now have the solution, you have to use the NuGet package:

    NuGet Install-Package System.Drawing.Common - Version 8.0.8

    and use the image from there.

            // using System.Drawing.Common (Nuget.org)
           /*
               // 
               // pictureBoxCompanyImage
               // 
               pictureBoxCompanyImage.Image = UserControlRuSplash.Resource1.Klaus72x72;
               pictureBoxCompanyImage.Location = new Point(112, 55);
               pictureBoxCompanyImage.Name = "pictureBoxCompanyImage";
               pictureBoxCompanyImage.Size = new Size(184, 148);
               pictureBoxCompanyImage.SizeMode = PictureBoxSizeMode.StretchImage;
               pictureBoxCompanyImage.TabIndex = 0;
               pictureBoxCompanyImage.TabStop = false;
          */
    
           [CategoryAttribute("RuSplash Settings")]
           [DescriptionAttribute("Company CompanyImage")]
           [Browsable(true)]
           public System.Drawing.Image CompanyImage { set; get; }
    
           private void RuSplash_ClientSizeChanged(object sender, EventArgs e)
           {
               if (CompanyImage != null)
               {
                   this.pictureBoxCompanyImage.Image = CompanyImage;
               }
           }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.