Can you open a JPEG, add text, and resave as a JPEG in .NET?

后端 未结 2 1025
不思量自难忘°
不思量自难忘° 2021-01-13 01:33

I want to write a small program in .NET 4.0 that will open a .jpg (or .jpeg) file, add a line of text to the image, and then resave the image as a .jpg. Does anyone know th

相关标签:
2条回答
  • 2021-01-13 02:10

    Something like this:

    var filePath = @"D:\Pictures\Backgrounds\abc.jpg";
    Bitmap bitmap = null;
    
    // Create from a stream so we don't keep a lock on the file.
    using (var stream = File.OpenRead(filePath))
    {
        bitmap = (Bitmap)Bitmap.FromStream(stream);
    }
    
    using (bitmap)
    using (var graphics = Graphics.FromImage(bitmap))
    using (var font = new Font("Arial", 20, FontStyle.Regular))
    {
        // Do what you want using the Graphics object here.
        graphics.DrawString("Hello World!", font, Brushes.Red, 0, 0);
    
        // Important part!
        bitmap.Save(filePath);
    }
    
    0 讨论(0)
  • 2021-01-13 02:25
    var myBitmap = new Bitmap("C:\\myImage.jpg");
    var g = Graphics.FromImage(myBitmap);
    g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));
    
    0 讨论(0)
提交回复
热议问题