简体   繁体   English

我在调试时遇到问题,我不知道 C# 的原因是什么

[英]i have an issue in debugging and i don't know whats is the reason C#

i have two images the first one for product , and the second one for Barcode of product .我有两张图片,第一张是产品,第二张是产品条形码。 i use to create product this code我用这个代码来创建产品

private void button1_Click(object sender, EventArgs e)
    {
        string barcode = CMBIDCAT.Text+"345" + TXTIDP.Text +"012" ;
        Bitmap bitmap = new Bitmap(barcode.Length * 20, 50);
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            Font ofont = new Font("code 128", 30);
            PointF point = new PointF(2f, 2f);
            SolidBrush blackbursh = new SolidBrush(Color.Black);
            SolidBrush whitebursh = new SolidBrush(Color.White);
            graphics.FillRectangle(whitebursh, 0, 0, bitmap.Width, bitmap.Height);
            graphics.DrawString("*" + barcode + "*",ofont, blackbursh, point);
        }
        using (DataTable dt = new DataTable())
        {
            PIC2.Image = bitmap;
            PIC2.Height = bitmap.Height;
            PIC2.Width = bitmap.Width;
        }
private void button3_Click(object sender, EventArgs e)
    {
        MemoryStream ms = new MemoryStream();
        PIC1.Image.Save(ms, PIC1.Image.RawFormat);
        byte[] byteImage = ms.ToArray();
        MemoryStream st = new MemoryStream();
        PIC2.Image.Save(st, PIC2.Image.RawFormat);
        byte[] byteImage1 = st.ToArray();

        prd.ADD_PRODUCT(Convert.ToInt32(CMBIDCAT.SelectedValue), TXTIDP.Text, TXTNMP.Text, Convert.ToInt32(TXTFP.Text), Convert.ToInt32(TXTSP.Text), Convert.ToInt32(TXTTP.Text), TXTDES.Text, Convert.ToInt32(TXTQTE.Text), byteImage, byteImage1);
        MessageBox.Show("تمت الاضافة بنجاح ", "عملية الاضافة",MessageBoxButtons.OK , MessageBoxIcon.Information);
    }

i don't know the reason for this issue please any one help me how can i solve this please .我不知道这个问题的原因,请任何人帮助我如何解决这个问题。

I don't have the rep to just comment.我没有代表只能发表评论。 But, should you be using a new MemoryStream for your second image?但是,您应该为第二张图片使用新的 MemoryStream 吗?

According to the MSDN you should not write to a stream that has been written to.根据 MSDN,您不应写入已写入的流。

MSDN "Image.Save Method (Stream, ImageFormat)" MSDN "Image.Save 方法 (Stream, ImageFormat)"

Change your code to:将您的代码更改为:

MemoryStream ms = new MemoryStream();
PIC1.Image.Save(ms, PIC1.Image.RawFormat);
byte[] byteImage = ms.ToArray();

MemoryStream ms1 = new MemoryStream();
PIC2.Image.Save(ms1, PIC2.Image.RawFormat);
byte[] byteImage1 = ms1.ToArray();

Hopefully that works.希望这有效。 :) :)

Make sure which value is null while debugging whether the Format or the stream, and consider using the "using" statement with memory stream in order for the resources to be freed like the following :确保在调试格式或流时哪个值为空,并考虑对内存流使用“using”语句,以便像下面这样释放资源:

        byte[] byteImage1;

        using(MemoryStream ms = new MemoryStream())
        {
            PIC1.Image.Save(ms, PIC1.Image.RawFormat);
            byteImage1 = ms.ToArray();
        }

And consider using two memory streams for each image as @Tim.E suggested.并考虑按照@Tim.E 的建议为每个图像使用两个内存流。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我不知道键和属性没有名称时,如何在 C# 中解析 JSON 对象? - How do I parse a JSON object in C# when I don't know key and properties don't have names? VS 2019 总是说,方法必须有返回类型”,我不知道怎么回事 - VS 2019 always says ,,Method must have a return type" and i don't know whats wrong C#NullReferenceException,但我不知道是什么原因引起的 - C# NullReferenceException, but I don't know what's causing it c#中不知道怎么减少嵌套代码 - I don't know how to reduce nesting code in c# 我需要做一个 python 项目,但我知道 C# 并且不知道如何转换它 - I need to make a python project but I know C# and don't know how to convert it 我正在用 Unity 研究 C# 构造函数和析构函数,但我不知道为什么它会这样工作 - I'm studying the C# constructor and destructor with Unity, but I don't know why it works like this 我不知道可以在哪里放置C#XML设置文件 - i don't know where i can put C# XML Setting File 如果我不知道GUID,如何使用c#在Microsoft CRM上检索一条记录? - How do I retrieve a single record on Microsoft CRM using c# if I don't know the GUID? 针对C#异步的秒表:我被秒表所迷惑了,还是有我不知道的东西? - Stopwatch against C# Async: Am I fooled by the stopwatch or there's something there I don't know? 当我事先不知道密钥时,如何在 C# 中解析 JSON 对象? - How do I parse a JSON object in C# when I don't know the key in advance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM