[英]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.