lapadets
lapadets

Reputation: 1095

Save text file contents as an image

I am trying to convert the contents of a text file to an image using C#, but I cannot seem to do it. I get the annoying A generic error occurred in GDI+. error from System.Drawing.

Here is my actual code:

public static Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
{
    Bitmap bmp = new Bitmap(width, Height);
    using (Graphics graphics = Graphics.FromImage(bmp))
    {
        Font font = new Font(fontname, fontsize);
        graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
        graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
        graphics.Flush();
        font.Dispose();
        graphics.Dispose();
    }
    bmp.Save("C:\\" + Guid.NewGuid().ToString() + ".bmp");
    Convert(bmp);
    return bmp;
}

So at the line bmp.Save("C:\\" + Guid.NewGuid().ToString() + ".bmp"); when I try to save the image, it brings this error. I read posts and similar questions and wrapped everything around a using statement as suggested at multiple sources, but I am still missing something and cannot figure it out.

I use the File.ReadAllText(path); to read the contents of the text file and then I make a normal call to my method:

ConvertTextToImage(content, "Bookman Old Style", 10, Color.White, Color.Black, width, height);

Upvotes: 1

Views: 1847

Answers (3)

Łukasz Rejman
Łukasz Rejman

Reputation: 1892

I think that your app has no permission to write files on C:/. A have modified your code and this works for me:

bmp.Save(Guid.NewGuid().ToString() + ".bmp");

It will save the image in your app folder.

Moreover, you don't have to call graphics.Dispose();, because using does it for you.

Upvotes: 0

Bernd Linde
Bernd Linde

Reputation: 2152

Due to UAC, your are getting the generic error because you are trying to write to the root directory of the C: drive, which is not allowed in Windows.

Changing the line

bmp.Save("C:\\" + Guid.NewGuid().ToString() + ".bmp");

to

bmp.Save("C:\\Temp\\" + Guid.NewGuid().ToString() + ".bmp");

(or any other folder that you have permissions for), allows the successful creation of the image.

Upvotes: 0

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

So, you're Dispose()ing your Graphics object twice. Once via your line that says graphics.Dispose() and once that is done automatically via the using construct. You should be fine with the method written as such:

public static Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
{
    var bmp = new Bitmap(width, Height);

    using (var graphics = Graphics.FromImage(bmp))
    using (var font = new Font(fontname, fontsize))
    {
        graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
        graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
    }

    bmp.Save("C:\\" + Guid.NewGuid() + ".bmp");
    Convert(bmp);
    return bmp;
}

Upvotes: 3

Related Questions