我遇到以下情況,需要將文本文件(文本文件蒸汽)轉換爲小圖像,然後將它們存儲在數據庫(SQL Server)中,以便稍後顯示在'預覽'屏幕中。任何人有任何建議如何做到這一點?特別是將文本文件轉換爲圖像?將文本文件轉換爲圖像以便在.NET中進行預覽
1
A
回答
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服用;)
+0
好的,我會嘗試一下這個代碼,但是你認爲什麼是解決問題的更好方法? –
+0
如果您僅顯示文本,則可能從數據庫中獲取該文本的片段,並在某種工具提示窗口中顯示該文本。 – niaher
相關問題
- 1. 將文本轉換爲jQuery實時預覽div中的圖像
- 2. 如何將PDF文檔轉換爲PHP中的預覽圖像?
- 3. 將文本從圖像轉換爲文本文件
- 4. Python OCR:將掃描圖像轉換爲文本進行處理
- 5. Xcode iPhone - 如何將圖像轉換爲文本進行操作?
- 6. 將.sh文件轉換爲批處理文件以便在windows中執行
- 7. 將MS word文件轉換爲.Net C中的圖像#
- 8. .NET將文件轉換爲圖像以存儲在數據庫中
- 9. 將文本文件中的矩陣轉換爲灰度圖像
- 10. 將文本文件轉換爲圖形
- 11. 將文本文件轉換爲單行
- 12. 將文本文件轉換爲一行
- 13. SPen將圖像轉換爲文本
- 14. 將文本轉換爲圖像
- 15. 將動態文本轉換爲圖像
- 16. 將圖像轉換爲文本 - Java
- 17. ABCpdf將文本轉換爲圖像
- 18. WPF將圖像轉換爲文本
- 19. 將文本轉換爲圖像php
- 20. 如何將文本轉換爲圖像?
- 21. 將圖像轉換爲文本
- 22. 如何將文本轉換爲圖像?
- 23. 將文本轉換爲圖像
- 24. HTML文件將其轉換爲本地圖像文件
- 25. 將二進制文件轉換爲C中的文本文件
- 26. 如何將E01圖像文件轉換爲dd圖像文件?
- 27. 使用R將PDF文件轉換爲文本文件進行文本挖掘
- 28. 可以在Javascript中將圖像文件轉換爲二進制格式。
- 29. 如何將圖像轉換爲Swift中的二進制文件
- 30. 如何將圖像轉換爲android中的文本轉換器?
你能告訴我們有關文本文件?它們是否包含Base64編碼的JPEG圖像? –
只是文本,我想轉換它,以便您可以看到第一頁作爲圖像,如截圖。 –