1

我有以下情况,我需要将文本文件(文本文件流)转换为小图像,然后将它们存储在数据库(SQL Server)中,以便以后可以在“预览”屏幕中显示。有人对如何做到这一点有任何建议吗?尤其是将文本文件转换为图像?

4

1 回答 1

1

我不认为将文本文件转换为图像是解决问题的最佳方法。话虽如此,这里有一些示例代码可以帮助您入门。

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

/// <summary>
/// A utility class to generate images from a string.
/// </summary>
public static class ImageMaker
{
    private static readonly ImageCodecInfo MyCodecInfo = GetEncoderInfo("image/jpeg");
    private static readonly EncoderParameters MyEncodeParameters = new EncoderParameters(1);

    static ImageMaker()
    {
        // Specify the quality for generated images.
        MyEncodeParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
    }

    /// <summary>
    /// Takes in a string and returns an image of that text.
    /// </summary>
    /// <param name="text">Text to put on an image.</param>
    /// <returns>Image instance.</returns>
    public static Image DrawText(string text)
    {
        // Create a bitmap to store the result.
        var destImage = new Bitmap(500, 700);

        // Create a graphic to draw the text on.
        Graphics graphic = Graphics.FromImage(destImage);
        graphic.FillRectangle(new SolidBrush(Color.White), 0, 0, destImage.Width, destImage.Height);

        graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        var fontFamily = new FontFamily("Arial");
        var textRectangle = new Rectangle(0, 0, destImage.Width, destImage.Height);
        var format = new StringFormat();
        float emSize = graphic.DpiY * 10 / 72;

        // Create text path.
        var path = new GraphicsPath();
        path.AddString(text, fontFamily, (int)FontStyle.Regular, emSize, textRectangle, format);

        // Write path to graphic.
        var fillBrush = new SolidBrush(Color.FromKnownColor(KnownColor.Black));
        graphic.FillPath(fillBrush, path);

        return destImage;
    }

    /// <summary>
    /// Writes image to a stream.
    /// </summary>
    /// <param name="image">Image instance.</param>
    /// <param name="stream">Stream instance.</param>
    public static void WriteToStream(Image image, Stream stream)
    {
        image.Save(stream, MyCodecInfo, MyEncodeParameters);
    }

    /// <summary>
    /// Gets codec info for the specified MIME type.
    /// </summary>
    /// <param name="mimeType">MIME type name.</param>
    /// <returns>ImageCodecInfo instance or null if no matching info was found.</returns>
    private static ImageCodecInfo GetEncoderInfo(string mimeType)
    {
        ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
        return encoders.FirstOrDefault(t => t.MimeType == mimeType);
    }
}

以下是它在 .ashx 处理程序中的使用方式:

public class GetPreview : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var image = ImageMaker.DrawText(
            @"Normally, both your asses would be dead as fucking fried chicken, " +
            "but you happen to pull this shit while I'm in a transitional period so " +
            "I don't wanna kill you, I wanna help you. But I can't give you this case, " +
            "it don't belong to me. Besides, I've already been through too much " +
            "shit this morning over this case to hand it over to your dumb ass.");

        context.Response.ContentType = "image/jpeg";
        ImageMaker.WriteToStream(image, context.Response.OutputStream);
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

干杯。

PS-lipsum 文本取自slipsum.com ;)

于 2012-11-15T15:22:50.510 回答