掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
VCLのDirectWriteで背景透過文字描画 (ID:151229)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
すいません、アルファチャネルは自力でPNGに入れる必要がありますね。 uses Winapi.D2D1, Vcl.Direct2D, Vcl.Imaging.pngimage; procedure TForm1.Button1Click(Sender: TObject); type TRGBQArray = array [0..High(Integer) div 4 - 1] of RGBQUAD; PRGBQArray = ^TRGBQArray; const //FileName:String = 'C:\Users\xxxx\ドキュメント\test.png'; FileName:String='a.png'; Text:String ='文字サンプル'; w:Integer=500; h:Integer=300; var D2DCanvas: TDirect2DCanvas; TextFormat: IDWriteTextFormat; TextLayout: IDWriteTextLayout; Bmp: TBitmap; Png: TPngImage; p: TD2D1Point2F; r: TRect; y,x:Integer; PQ: PRGBQArray; AlphaRow: PByteArray; begin Bmp := TBitmap.Create; Bmp.PixelFormat:=pf32bit; //Bmp.Transparent := true; Bmp.AlphaFormat := afDefined; Bmp.SetSize(w,h); r := Rect(0,0,w,h); {1ピクセルずつアルファチャンネルの値を設定} for y := 0 to BMP.Height - 1 do begin PQ := BMP.ScanLine[y];//RGBQuadの配列へのポインタを代入する for x := 0 to BMP.Width - 1 do begin TRGBQuad(PQ[x]).rgbReserved := 0;//アルファチャンネル TRGBQuad(PQ[x]).rgbBlue:=255; TRGBQuad(PQ[x]).rgbGreen:=0; TRGBQuad(PQ[x]).rgbRed:=0; end; end; D2DCanvas := TDirect2DCanvas.Create(Bmp.Canvas,r); D2DCanvas.BeginDraw; DWriteFactory.CreateTextFormat(PWideChar('メイリオ'),nil,DWRITE_FONT_WEIGHT_NORMAL,DWRITE_FONT_STYLE_NORMAL,DWRITE_FONT_STRETCH_NORMAL,40,'ja-jp',TextFormat); DWriteFactory.CreateTextLayout(PWideChar(Text),Length(Text),TextFormat,0,0,TextLayout); TextLayout.SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); D2DCanvas.Font.Brush.Color := clBlue; p.x := 100; p.y := 100; D2DCanvas.RenderTarget.DrawTextLayout(p,TextLayout,D2DCanvas.Font.Brush.Handle,D2D1_DRAW_TEXT_OPTIONS_NONE); D2DCanvas.EndDraw; D2DCanvas.Free; Png := TPngImage.Create; //Png.Transparent:=True; Png.Assign(Bmp); //アルファ チャネル情報を追加 Png.CreateAlpha; //アルファチャンネルはPNGに自力で入れる for y := 0 to BMP.Height - 1 do begin PQ := BMP.ScanLine[y]; AlphaRow:=Png.AlphaScanline[y]; for x := 0 to BMP.Width - 1 do begin AlphaRow[x]:=TRGBQuad(PQ[x]).rgbReserved;//アルファチャンネル end; end; Png.SaveToFile(FileName); Png.Free; Bmp.Free; end;
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.