簡體   English   中英

使用.DrawToBitmap - 如何更改圖像的分辨率?

[英]Using .DrawToBitmap - how to change resolution of image?

我使用DrawToBitmap將一些標簽保存為圖像。 我想知道如何改變這些圖像的分辨率,有沒有辦法? 假設我有一個帶文本的標簽,我想將其渲染為圖像文件(不發布完整代碼):

this.label1 = new System.Windows.Forms.Label();
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 36F);
//...
this.label1.Size = new System.Drawing.Size(161, 54);
this.label1.Text = "Output";
//...

//save image:
Bitmap image = new Bitmap(161, 54);
this.label1.DrawToBitmap(image, this.label1.ClientRectangle);
image.Save(@"C:\image.jpg");

這工作正常,我會得到這樣的東西:

結果圖片

分辨率還可以,但可以增加嗎? 當我稍微放大這個圖像時,我可以將各個像素視為大塊:

縮放結果圖像

我知道這是正常的,因為它不是矢量圖形,那很好。 我只想以某種方式更改它,以便您可以在將單個像素視為大塊之前進一步放大。 有任何想法嗎?

謝謝。

編輯:如果我只使用黑/白圖像 - 將圖像保存為png或gif可能更好嗎?

像這樣的東西可以完成這項工作,只需增加標簽使用的字體大小:

    Bitmap CreateBitmapImage(string text, Font textFont, SolidBrush textBrush)
    {
        Bitmap bitmap = new Bitmap(1, 1);
        Graphics graphics = Graphics.FromImage(bitmap);
        int intWidth = (int)graphics.MeasureString(text, textFont).Width;
        int intHeight = (int)graphics.MeasureString(text, textFont).Height;
        bitmap = new Bitmap(bitmap, new Size(intWidth, intHeight));
        graphics = Graphics.FromImage(bitmap);
        graphics.Clear(Color.White);
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.DrawString(text, textFont, textBrush,0,0);
        graphics.Flush();
        return (bitmap);
    }

也許嘗試一種不同的方法,就像這里描述的那樣

你可以通過增加System.Drawing.Font的第二個參數的值來實現這一點。

this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 1000F);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM