Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application written in vb .net that calls a usercontrol I wrote in C#. No problem there. My need is to send the usercontrol an image or bitmap or memory stream. I've tried them all.

I would also like to be able to debug the usercontrol while running the winform app. I used to do this all the time and forgot how. I have searched the internet and I get alot of jibberish. I remember it being quite straight forward but forgot how.

Where the #%%$$$ is the link to upload an example?

What I have tried:

Attached is a small test app and a usercontrol.

The testapp requests an image through a typical dialog and then calls the usercontrol, The strings pass fine from the user control back to winforms but I can't seam to pass an image from the winform once loaded in the picturebox on the winform to the picturebox in the usercontrol. My real usercontrol is a blackbox. I don't need to see the images just pass them to, process and send back.


Here is the main code from the user control:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
namespace MyUserControl
{
    public partial class UserControl1: UserControl
    {
        private MemoryStream mImageIn;
        private string mStrOut;
        public MemoryStream PicIn
        {
            get
            {
                return mImageIn;
            }
            set
            {
                mImageIn = value;
            }
        }
        public string strPicOut
        {
            get
            {
                return mStrOut;
            }
            set
            {
                mStrOut = value;
            }
        }
        public UserControl1()
        {
            InitializeComponent();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }
        public new void Update()
        {
            // public static System.Drawing.Image FromStream(System.IO.Stream stream);
            Image img = Image.FromStream(PicIn);


            pictureBox1.Image = img;
            MessageBox.Show("Hello There");
            strPicOut = "Whats Not Up";
        }

       
    }
}


Here is the code from the Windows form app:

Imports MyUserControl
Imports System.Drawing.Imaging
Imports System.IO

Public Class Form1
    Private UI1 As New UserControl1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strfilename As String
        OpenFileDialog1.Filter = "All Images Files (*.png;*.jpeg;*.gif;*.jpg;*.bmp;*.tiff;*.tif _
         | *.png;*.jpeg;*.gif;*.jpg;*.bmp;*.tiff;" & "*.tif"
        Dim OKDK As Boolean = OpenFileDialog1.ShowDialog
        If OKDK Then
            strfilename = OpenFileDialog1.FileName
            PictureBox1.Image = Image.FromFile(strfilename)
            Dim bmp As Bitmap = PictureBox1.Image

            Dim Picstream As MemoryStream = New MemoryStream
            bmp.Save(Picstream, ImageFormat.Bmp)
            UI1.PicIn = Picstream

            UI1.Update()
            Label2.Text = UI1.strPicOut
        End If
        Cursor = Cursors.Default
    End Sub

    Private Sub UserControl11_Load(sender As Object, e As EventArgs) Handles ImgTest.Load

    End Sub
End Class




Any help would be greatly appreciated. This app is due friday and I have been fighting all kinds of stupds 24 hours a day for the last several weeks. Totally stressed.

Larry
Posted
Updated 27-Nov-19 9:01am
v3

1 solution

Just reference the UserControl project instead of the DLL in your WinForm app, and when you debug your main app, you will be able to debug straight into your control as well.


[edit]
OK - I created a Test solution.
Added a ClassLibrary project, deleted the default Class1, added a UserControl called DemoUserControl, dropped a PictureBox on that, set it to Dock/Fill. Added a property called Image to the Control:
C#
namespace DemoClassLibrary
    {
    public partial class DemoUserControl : UserControl
        {
        public Image Image
            {
            get
                {
                return MyPictureBox.Image;
                }
            set
                {
                MyPictureBox.Image = value;
                }
            }
        public DemoUserControl()
            {
            InitializeComponent();
            }
        }
    }

Added a WinForms project to the solution called DemoForm, set it as the Startup project. Added a reference to DemoClassLibrary.
Built the solution.
Dropped a DemoUserControl onto the form in the designer.
Added a button, handled it's Click event:
C#
private void MyButton_Click(object sender, EventArgs e)
    {
    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() == DialogResult.OK)
        {
        Image image = Bitmap.FromFile(ofd.FileName);
        MyDemoUserControl.Image = image;
        }
    }

Added a second button, handled it's Click event:
C#
private void MyGetButton_Click(object sender, EventArgs e)
    {
    Image image = MyDemoUserControl.Image;
    MessageBox.Show($"{image.Height}x{image.Width}");
    }

Ran it.
Click button one, select an image, it shows.
Click button two, it shows the size.
Repeat - it works.
It's not perfect - you can't open the same image twice because it's locked as I don't Dispose my Bitmaps - but it works. Every time.

So what have I done that is different to what you have done?

[/edit]
 
Share this answer
 
v2
Comments
larry118 27-Nov-19 16:42pm    
A little more info please. I can add the usercontrol project to the windows app but cannot debug the usercontrol. Can you explain what you mean by "reference"
Afzaal Ahmad Zeeshan 27-Nov-19 22:15pm    
You might have to rebuild the project for the control to be visible.

See more on this here.
larry118 28-Nov-19 2:16am    
I got the debug to work and I can see the variables I passed to the user control method Update but in the method the controls I am trying to assign the values don't do anything:

UserControl Code:

using System.IO;

namespace MyUserControl
{

public partial class UserControl1 : UserControl
{
private MemoryStream mImageIn;
private string mStrOut;
private string mStrIn;
public MemoryStream PicIn
{
get
{
return mImageIn;
}
set
{
mImageIn = value;
}
}
public string strPicOut
{
get
{
return mStrOut;
}
set
{
mStrOut = value;
}
}
public string strPicIn
{
get
{
return mStrIn;
}
set
{
mStrIn = value;
}
}
public UserControl1()
{
InitializeComponent();
}


public void Update(MemoryStream PicIn, string str )
{

Image img = Image.FromStream(PicIn);


this.pictureBox1.Image = img;
MessageBox.Show("Hello There");
this.strPicOut = "Whats Not Up";
txtBox1.Text = str;
}


}
}
OriginalGriff 28-Nov-19 7:36am    
The normal reason for that is you aren't using the displayed instance of the UserControl - check the code that calls Update and see exactly which instance you are trying to update. Remember: you cna have many copies of a UserControl on a single form, so you have to specify the exact one on the display you want to use. If you say

x = new MyUserControl;
x.Update(...);

It won't update anything.
larry118 28-Nov-19 10:43am    
Happy Thanksgiving. I appreciate your response but that's not it. Here is how I am declaring the user control:
Imports MyUserControl
Imports System.Drawing.Imaging
Imports System.IO



Public Class Form1

Public UI1 As New UserControl1
Public Sub New()
Now that I have debug working I can see the data is getting to the Update Method. The stream is converted back to a bitmap but the assignment of the text and bitmap to their respective controls is not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900