Question PATH for StreamReader

Ivo

Member
Joined
Feb 8, 2022
Messages
18
Programming Experience
Beginner
Hoe kan ik hier een correct pad voor maken?
het werkt zo, maar als ik een string als pad gebruik, werkt het niet, fout zeg ACCEPT NO NULL

ik ben een beginner



Streamreader PATH:
openbare gedeeltelijke klasse frmQRcode : Form
{
openbare string tbDoorgeef { get; set; } // transfered from other FORM
openbare frmQRcode()
{
InitialiseerComponent();
GenereerQRcode();
}


privé async ongeldig GenereerQRcode()
{
met behulp van ( StreamReader sr = nieuwe StreamReader (tbDoorgeef )) // This dont work !! ("test.txt ) works fine
{
tbTextWindow.Text = wacht op sr.ReadToEndAsync();
}
BarcodeWriter barcodeWriter = nieuwe BarcodeWriter();
EncodingOptions encodingOptions = new EncodingOptions() {Breedte = 541, Hoogte = 541, Marge = 0,PureBarcode=false};
encodingOptions.Hints.Add (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
barcodeWriter.Renderer = nieuwe BitmapRenderer();
barcodeWriter.Options = encodingOptions;
barcodeWriter.Format = BarcodeFormat.QR_CODE;
Bitmap bitmap = barcodeWriter.Write(tbTextWindow.Text); //(tbTextWindow.Text);
Bitmap-logo = nieuwe Bitmap ($"{Application.StartupPath}/EMG_VAR_QRlogo.png");
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(logo, new Point((bitmap.Width - logo.Width) /2, (bitmap.Height - logo.Height)/02));
pbQRcode.Afbeelding = bitmap;
}
 
Last edited:
What you have shown will work if done properly. If it's not working for you then you haven't done it properly. As you have omitted relevant parts, we can't tell you exactly what you've done wrong. Please post all the relevant code and please wrap it in appropriate formatting tags.

Also, we're not going to email you. The whole point of a public forum is that we discuss the issue here, where everyone can see, help and benefit. If you post your email address in public like that, expect spammers to harvest it and bombard you with spam.
 
Thanks for the reply

I have put my code as you asked

when i use a string as PATH, it dont work, even the string contains test.txt, when i put "test.txt", it works

Thanks in advance
 
By proper formatting, what @jmcilhinney meant was using code tags. E.g. the </> button that you see on the toolbar above the text posting field.

As for your issue, step through the code with a debugger. Verify that you have the correct value by the time your get to that point in the code.
 
I found myself
This code is working !





I found it out myself:
namespace Var_List
{
   
    public partial class frmQRcode : Form
    {
       public string Path { get; set; }
       
        public frmQRcode()
        {
            InitializeComponent();
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private  void frmQRcode_Load(object sender, EventArgs e)
        {
            GenereerQRcode(Path);
        }

        private async void GenereerQRcode(string DoorgeefPath)
        {
            tbPath.Text = Path;
            using (StreamReader sr = new StreamReader(DoorgeefPath))
            {
                tbTextWindow.Text = await sr.ReadToEndAsync();
            }
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            EncodingOptions encodingOptions = new EncodingOptions() { Width = 541, Height = 541, Margin = 0, PureBarcode = false };
            encodingOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            barcodeWriter.Renderer = new BitmapRenderer();
            barcodeWriter.Options = encodingOptions;
            barcodeWriter.Format = BarcodeFormat.QR_CODE;
            Bitmap bitmap = barcodeWriter.Write(tbTextWindow.Text); //(tbTextWindow.Text);
            Bitmap logo = new Bitmap($"{Application.StartupPath}/EMG_VAR_QRlogo.png");
            Graphics g = Graphics.FromImage(bitmap);
            g.DrawImage(logo, new Point((bitmap.Width - logo.Width) / 2, (bitmap.Height - logo.Height) / 02));
            pbQRcode.Image = bitmap;
        }

    }
}
 
Last edited:
Amusingly, in the code I see here most C# keywords have been translated to Dutch. Not sure what is happening here, I've never seen this happen before. Maybe it's because the header says "Streamreader PATH" instead of "C#" ?

It would seem obvious that new StreamReader (tbDoorgeef) is throwing a null exceptionto because the variable tbDoorgeef has not been given a value.

Probably unrelated, but I notice this line in the code:

Bitmap-logo = new Bitmap ($"{Application.StartupPath}/EMG_VAR_QRlogo.png");

How did that ever get past the compiler ? The minus sign is not valid here.
 
Amusingly, in the code I see here most C# keywords have been translated to Dutch. Not sure what is happening here, I've never seen this happen before. Maybe it's because the header says "Streamreader PATH" instead of "C#" ?

It would seem obvious that new StreamReader (tbDoorgeef) is throwing a null exceptionto because the variable tbDoorgeef has not been given a value.

Probably unrelated, but I notice this line in the code:

Bitmap-logo = new Bitmap ($"{Application.StartupPath}/EMG_VAR_QRlogo.png");

How did that ever get past the compiler ? The minus sign is not valid here.

Amusingly, in the code I see here most C# keywords have been translated to Dutch. Not sure what is happening here, I've never seen this happen before. Maybe it's because the header says "Streamreader PATH" instead of "C#" ?

It would seem obvious that new StreamReader (tbDoorgeef) is throwing a null exceptionto because the variable tbDoorgeef has not been given a value.

Probably unrelated, but I notice this line in the code:

Bitmap-logo = new Bitmap ($"{Application.StartupPath}/EMG_VAR_QRlogo.png");

How did that ever get past the compiler ? The minus sign is not valid here.
I realy dont know why its translated, looks strange and weird.....
And yes, it passed the compiler, its a UNDERSCORE no MINUS !
QR.png
 
And yes, it passed the compiler, its a UNDERSCORE no MINUS !
No !! Look again , it was a hyphen, not an underscore.
Bitmap-logo = new Bitmap ($"{Application.StartupPath}/EMG_VAR_QRlogo.png");
But I see you have taken it out which was the right thing.
And it definitely helps if you pass your parameters.
 
This:
C#:
using (StreamReader sr = new StreamReader(DoorgeefPath))
{
    tbTextWindow.Text = await sr.ReadToEndAsync();
}

could simply have been:
C#:
tbTextWindow.Text = File.ReadAllText(DoorgeefPath);

since you are not using the async/await in any meaningful way, as well as the maximum data that can go into a QR code is less than 8KB (assuming all numbers) or a little over 4KB (assuming alphanumeric) data and can be read very quickly even with old hardware that supports the .NET Framework or .NET Core.
 
From what I can see from your non-working code and now your working code, the issue was that in your old code you were calling the QR code generator in your constructor passing in the value of the property. Of course, the property would be null at that point in time because the class had just been constructed.

A workaround would have been to declare a constructor for your class where you could pass in the path, and then set the property or better yet, just remove the property and just pass along that path to your code for generating the QR code. I've been assured by other people on this forum that current versions of the WinForms Designer now supports forms which constructor parameters, but I've not personally tried lately since I just hand code my forms.

Another workaround is to have the setter of your path property call your code to generate the QR code. This depends on which camp you belong to regarding whether properties are allowed to do work; or if they are just supposed to hold values; or somewhere in between where properties can do work as long as it not "expensive work".
 
Last edited:

Latest posts

Back
Top Bottom