Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ,
C#
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 );
}
Posted
Updated 16-Apr-10 2:36am
v2

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.


 
Share this answer
 
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();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900