文件不能释放问题请看代码.
public void AttachText(string file, string logo, string logoType,int x,int y)
{
MemoryStream s = new MemoryStream();
Image img = Image.FromFile(file);
Graphics graphics = Graphics.FromImage(img);
graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, img.Width, img.Height));
graphics.SmoothingMode = SmoothingMode.HighQuality;
int nlen = System.Text.Encoding.Default.GetByteCount(logo);
graphics.DrawString(logo, new Font( "Arial ", 52, GraphicsUnit.Pixel), Brushes.Black, new Point(x, y));
img.Save(s, img.RawFormat);
graphics.Dispose();
img.Dispose();
FileStream fi = new FileStream(file, FileMode.Open, FileAccess.ReadWrite);
s.WriteTo(fi);
s.Close();
fi.Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
AttachText( "C:\\abc.jpg ",textBox1.Text, "Text ",40,40);
pictureBox1.Image = Image.FromFile( "c:\\abc.jpg ");
}
功能是替换图片文件中的图片,点一次按钮就把输入的文字以图片的形式写到文件中,
点按钮的时候调用函数AttachText,第一次的时候可以成功,第二次FileStream fi = new FileStream(file, FileMode.Open, FileAccess.ReadWrite);这里就抛异常了,说的是文件正在使用,请问应该怎么释放啊.谢谢
------解决方案--------------------AttachLogoOnBitmap( "C:\\abc.jpg ",textBox1.Text, "Text ",40,40);
Byte[] bytBLOBData = new Byte[s.Length];
s.Position = 0;
s.Read(bytBLOBData, 0, Convert.ToInt32(s.Length));
MemoryStream stmBLOBData = new MemoryStream(bytBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);;