位图路径错误 [英] Bitmap Path error

查看:94
本文介绍了位图路径错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,
我试图将编辑后的图像以相同的名称存储在相同的位置.当我尝试将位图保存在加载它的相同位置时,会发生问题.您能帮我如何编辑图像吗?
这是代码,

Hi Folks,
I am trying to store the edited image at the same location with the same name. the problem occurs when I try to save the bitmap at the same location where I load it. Can you please help how can I edit the image.
Here is the code ,

public static void  insertTextInImage(string Path,string Text)
{
    Bitmap myBitmap = new Bitmap(Path);
    Graphics g = Graphics.FromImage(myBitmap);
    StringFormat strFormat = new StringFormat();
    strFormat.Alignment = StringAlignment.Center;
    g.DrawString(Text, new Font("Tahoma", 20), Brushes.Black ,
              new RectangleF(0, 0, 500, 500), strFormat);
    myBitmap.Save(Path );
}

推荐答案

我敢打赌,由于它仍处于打开状态,您无法保存它.在代码周围放置一个try/catch块,看看确切的异常是什么.到那时,您应该能够确定解决问题的最佳方法.


I bet you can''t save it because it''s still open. Put a try/catch block around your code and see what the exact exception is. At that point, you should be able to discern the best approach to fixing the problem.



尝试一下:)首先将位图保存到内存流中,然后将其写入文件流(读取:到原始文件).

Try this :) It saves the bitmap first into a memorystream and that writes it to a filestream (read: to the original file).

MemoryStream ms = new MemoryStream();

Bitmap myBitmap = new Bitmap(Path);

Graphics g = Graphics.FromImage(myBitmap);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;

g.DrawString(Text, new Font("Tahoma", 20), Brushes.Black, new RectangleF(0, 0, 500, 500), strFormat);

myBitmap.Save(ms, myBitmap.RawFormat);

FileStream fs = new FileStream(Path, FileMode.Create);

ms.WriteTo(fs);

fs.Close();


这篇关于位图路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆