由文本產生透明的文本圖片

來源:互聯網
上載者:User

這個函數使用GDI plus實現將文本進行簡單的排版,寫入記憶體DC ,儲存為透明的png圖片。
文字格式設定自動根據行寬分行,並保持原有換行格式。

//一些定義
#define BITMAP_WIDTH = 256;
#define BITMAP_HEIGHT = 256;
#define MAX_TEXT_WIDTH  240 // must <= BITMAP_WIDTH
#define MAX_TEXT_HEIGHT  240 // must <= BITMAP_HEIGHT

//函式宣告

void DrawFont(
   CStringW dstFilePath,//儲存檔案的全路徑
   CStringW text, // 要寫入的文本
   float size, // 字型大小
   UINT32 fgcolor = 0xffffffff,//字型顏色
   UINT32 bgcolor = 0x00000000,//背景顏色,透明mask用
   CStringW family = L"MS Pゴシック" //字型
   );

函數實現

void PiaResUtils::DrawFont(
 CStringW dstFilePath,
 CStringW text,
 float size,
 UINT32 fgcolor,
 UINT32 bgcolor,
 CStringW family)
{
 CStringW str = text;

 Status ret = Ok;
 FontFamily fontFamily(family);
 Font font(&fontFamily, size, FontStyleRegular, UnitPixel);
 Graphics g(0, 0);
 UINT32 width = BITMAP_WIDTH;
 UINT32 height = BITMAP_HEIGHT;
 INT32 pitch = INT32(width * 4);

 RectF fontRect;
 ret = g.MeasureString(L"函", -1, &font, RectF(0, 0, 0, 0), &fontRect);
 UINT32 fontH = (UINT32) (fontRect.Height);
 UINT32 fontW = (UINT32) (fontRect.Width);
 UINT32 maxCharInLine = (UINT32)(MAX_TEXT_WIDTH/fontW);

 std::auto_ptr<Bitmap> bitmap(new Bitmap(width, height, &g));
 {
  std::auto_ptr<Graphics> g(Graphics::FromImage(bitmap.get()));
  g->Clear(Color(ARGB(bgcolor)));
  g->SetTextRenderingHint(TextRenderingHintAntiAlias);

  PointF pointF;
  pointF.X = (BITMAP_WIDTH - MAX_TEXT_WIDTH)/2;
  pointF.Y = (BITMAP_HEIGHT - MAX_TEXT_HEIGHT)/2;
  SolidBrush solidBrush = Color(ARGB(fgcolor));

  UINT32 pos = 0;
  UINT32 len = 0;
  RectF rect;
  UINT32 strW = 0;
  CStringW temp = L"";
  while (str != L"") {
   pos = str.Find(L'/n');
   len = str.GetLength();
   if (pos == -1) {
    pos = str.GetLength();
    if (pos <= maxCharInLine) {
     temp = str;
     str = L"";
    }
    else {
     pos = maxCharInLine;

     BOOL ret = FALSE;
     while (!ret) {
      temp = str.Left(pos);
      ret = g->MeasureString(temp, -1, &font, RectF(0, 0, 0, 0), &rect);
      if ((UINT32) (rect.Width) <= MAX_TEXT_WIDTH) {
       pos += 1;
       if (pos > len) {
        pos = len;
        ret = TRUE;
       }
      }
      else {
       pos -= 1;
       ret = TRUE;
      }
     }

     temp = str.Left(pos);
     str = str.Right(len - pos);
    }
   }
   else if (pos <= maxCharInLine) {
    temp = str.Left(pos);
    if (len - pos - 1 > 0) {
     str = str.Right(len - pos - 1);
    }
    else {
     str = L"";
    }
   }
   else if (pos > maxCharInLine) {
    UINT32 posTo = maxCharInLine;
    BOOL ret = FALSE;
    while (!ret) {
     temp = str.Left(posTo);
     ret = g->MeasureString(temp, -1, &font, RectF(0, 0, 0, 0), &rect);
     if ((UINT32) (rect.Width) <= MAX_TEXT_WIDTH) {
      posTo += 1;
      if (posTo > pos) {
       posTo = pos;
       ret = TRUE;
      }
     }
     else {
      posTo -= 1;
      ret = TRUE;
     }
    }

    temp = str.Left(posTo);
    str = str.Right(len - posTo);
   }

   g->DrawString(temp, -1, &font, pointF, &solidBrush);
   
   pointF.Y += fontH;
   if ((UINT32)(pointF.Y + fontH )> MAX_TEXT_HEIGHT){
    str = L"";
   }
  }
 }
 {
  Rect rect(0, 0, width, height);
  BitmapData data;
  Status ret = bitmap->LockBits(&rect, ImageLockModeRead, PixelFormat32bppARGB, &data);
  if (ret != Ok) {
   throw std::exception("Gdiplus::Bitmap::LockBits failed.");
  }

  bitmap->UnlockBits(&data);
 }

 CLSID clsid;
 GetEncoderClsid(L"image/png", &clsid);
 bitmap->Save(dstFilePath, &clsid);
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.