Author: h.mohri
Delphiの場合
Delphiでは TBitmapの constructor
は別名で複数持っています。
CreateFromBitmapAndMask, CreateFromFile, CreateFromStream
1 2 3 4 5 6 7 8 9 10 11 |
<span class="k" style="font-weight:bold;">procedure</span> <span class="nc" style="color:#445588;font-weight:bold;">TForm1</span><span class="o" style="font-weight:bold;">.</span><span class="nf" style="color:#990000;font-weight:bold;">FormCreate</span><span class="p">(</span><span class="n">Sender</span><span class="o" style="font-weight:bold;">:</span> <span class="kt" style="color:#445588;font-weight:bold;">TObject</span><span class="p">)</span><span class="o" style="font-weight:bold;">;</span> <span class="k" style="font-weight:bold;">var</span> <span class="n">src_bmp</span><span class="o" style="font-weight:bold;">,</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">:</span> <span class="n">TBitmap</span><span class="o" style="font-weight:bold;">;</span> <span class="n">output_bmp</span><span class="o" style="font-weight:bold;">:</span> <span class="n">TBitmap</span><span class="o" style="font-weight:bold;">;</span> <span class="k" style="font-weight:bold;">begin</span> <span class="n">src_bmp</span> <span class="o" style="font-weight:bold;">:=</span> <span class="n">TBitmap</span><span class="o" style="font-weight:bold;">.</span><span class="n">CreateFromFile</span><span class="p">(</span><span class="s" style="color:#d01040;">'元のPNG'</span><span class="p">)</span><span class="o" style="font-weight:bold;">;</span> <span class="n">mask_bmp</span> <span class="o" style="font-weight:bold;">:=</span> <span class="n">TBitmap</span><span class="o" style="font-weight:bold;">.</span><span class="n">CreateFromFile</span><span class="p">(</span><span class="s" style="color:#d01040;">'マスク用のBMP'</span><span class="p">)</span><span class="o" style="font-weight:bold;">;</span> <span class="n">output_bmp</span> <span class="o" style="font-weight:bold;">:=</span> <span class="n">TBitmap</span><span class="o" style="font-weight:bold;">.</span><span class="n">CreateFromBitmapAndMask</span><span class="p">(</span><span class="n">src_bmp</span><span class="o" style="font-weight:bold;">,</span> <span class="n">mask_bmp</span><span class="p">)</span><span class="o" style="font-weight:bold;">;</span> <span class="n">output_bmp</span><span class="o" style="font-weight:bold;">.</span><span class="n">SaveToFile</span><span class="p">(</span><span class="s" style="color:#d01040;">'保存.PNG'</span><span class="p">)</span><span class="o" style="font-weight:bold;">;</span> <span class="c1" style="color:#999988;font-style:italic;">//出力されたPNGは元のPNGがマスク処理されている。</span> <span class="k" style="font-weight:bold;">end</span><span class="o" style="font-weight:bold;">;</span> |
C++Builderの場合
C++BuilderではCreateFromBitmapAndMaskは無く同名のconstructor
で処理できます。
下記述したC++コードはbcc32
です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<span class="cp" style="color:#999999;font-weight:bold;font-style:italic;">#include</span> <span class="cpf"><memory></span> <span class="k" style="font-weight:bold;">namespace</span> <span class="n">rect_helper</span> <span class="p">{</span> <span class="k" style="font-weight:bold;">template</span> <span class="o" style="font-weight:bold;"><</span><span class="k" style="font-weight:bold;">typename</span> <span class="n">T1</span><span class="p">,</span> <span class="k" style="font-weight:bold;">typename</span> <span class="n">T2</span><span class="o" style="font-weight:bold;">></span> <span class="n">T1</span> <span class="n">pmv</span><span class="p">(</span><span class="n">T1</span> <span class="n">src</span><span class="p">,</span> <span class="n">T2</span> <span class="n">left</span><span class="p">,</span> <span class="n">T2</span> <span class="n">buttom</span><span class="p">)</span> <span class="p">{</span> <span class="k" style="font-weight:bold;">return</span> <span class="n">T1</span><span class="p">(</span><span class="n">left</span><span class="p">,</span><span class="n">buttom</span><span class="p">,</span><span class="n">src</span><span class="p">.</span><span class="n">right</span> <span class="o" style="font-weight:bold;">+</span> <span class="n">left</span><span class="p">,</span> <span class="n">src</span><span class="p">.</span><span class="n">Bottom</span> <span class="o" style="font-weight:bold;">+</span> <span class="n">buttom</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="kt" style="color:#445588;font-weight:bold;">void</span> <span class="kr" style="font-weight:bold;">__fastcall</span> <span class="n">TForm1</span><span class="o" style="font-weight:bold;">::</span><span class="n">Button1Click</span><span class="p">(</span><span class="n">TObject</span> <span class="o" style="font-weight:bold;">*</span><span class="n">Sender</span><span class="p">)</span> <span class="p">{</span> <span class="k" style="font-weight:bold;">const</span> <span class="n">String</span> <span class="n">png_savefname</span> <span class="o" style="font-weight:bold;">=</span> <span class="s" style="color:#d01040;">"save.png"</span><span class="p">;</span> <span class="n">std</span><span class="o" style="font-weight:bold;">::</span><span class="n">tr1</span><span class="o" style="font-weight:bold;">::</span><span class="n">shared_ptr</span><span class="o" style="font-weight:bold;"><</span><span class="n">TBitmap</span><span class="o" style="font-weight:bold;">></span> <span class="n">src_png</span><span class="p">(</span><span class="k" style="font-weight:bold;">new</span> <span class="n">TBitmap</span><span class="p">(</span><span class="n">Edit1</span><span class="o" style="font-weight:bold;">-></span><span class="n">Text</span><span class="p">));</span> <span class="n">TRectF</span> <span class="n">src_rect</span> <span class="o" style="font-weight:bold;">=</span> <span class="n">TRectF</span><span class="p">(</span><span class="mi" style="color:#009999;">0</span><span class="p">,</span><span class="mi" style="color:#009999;">0</span><span class="p">,</span><span class="n">src_png</span><span class="o" style="font-weight:bold;">-></span><span class="n">Width</span><span class="p">,</span><span class="n">src_png</span><span class="o" style="font-weight:bold;">-></span><span class="n">Height</span><span class="p">);</span> <span class="c1" style="color:#999988;font-style:italic;">//Make TBitmap for mask.</span> <span class="n">std</span><span class="o" style="font-weight:bold;">::</span><span class="n">tr1</span><span class="o" style="font-weight:bold;">::</span><span class="n">shared_ptr</span><span class="o" style="font-weight:bold;"><</span><span class="n">TBitmap</span><span class="o" style="font-weight:bold;">></span> <span class="n">mask_bmp</span><span class="p">(</span><span class="k" style="font-weight:bold;">new</span> <span class="n">TBitmap</span><span class="p">(</span><span class="n">src_png</span><span class="o" style="font-weight:bold;">-></span><span class="n">Width</span><span class="p">,</span><span class="n">src_png</span><span class="o" style="font-weight:bold;">-></span><span class="n">Height</span><span class="p">));</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">BeginScene</span><span class="p">();</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">Fill</span><span class="o" style="font-weight:bold;">-></span><span class="n">Kind</span> <span class="o" style="font-weight:bold;">=</span> <span class="n">TBrushKind</span><span class="o" style="font-weight:bold;">::</span><span class="n">Solid</span><span class="p">;</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">Fill</span><span class="o" style="font-weight:bold;">-></span><span class="n">Color</span> <span class="o" style="font-weight:bold;">=</span> <span class="n">TAlphaColorRec</span><span class="o" style="font-weight:bold;">::</span><span class="n">Null</span><span class="p">;</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">FillRect</span><span class="p">(</span><span class="n">src_rect</span><span class="p">,</span> <span class="mi" style="color:#009999;">0</span><span class="p">,</span> <span class="mi" style="color:#009999;">0</span><span class="p">,</span> <span class="n">AllCorners</span><span class="p">,</span> <span class="mi" style="color:#009999;">1</span><span class="p">);</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">Fill</span><span class="o" style="font-weight:bold;">-></span><span class="n">Kind</span> <span class="o" style="font-weight:bold;">=</span> <span class="n">TBrushKind</span><span class="o" style="font-weight:bold;">::</span><span class="n">Solid</span><span class="p">;</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">Fill</span><span class="o" style="font-weight:bold;">-></span><span class="n">Color</span> <span class="o" style="font-weight:bold;">=</span> <span class="n">TAlphaColorRec</span><span class="o" style="font-weight:bold;">::</span><span class="n">White</span><span class="p">;</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">FillEllipse</span><span class="p">(</span><span class="n">src_rect</span><span class="p">,</span> <span class="mf" style="color:#009999;">1.0</span><span class="p">);</span> <span class="n">mask_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">EndScene</span><span class="p">();</span> <span class="c1" style="color:#999988;font-style:italic;">//CreateFromBitmapAndMask</span> <span class="n">std</span><span class="o" style="font-weight:bold;">::</span><span class="n">tr1</span><span class="o" style="font-weight:bold;">::</span><span class="n">shared_ptr</span><span class="o" style="font-weight:bold;"><</span><span class="n">TBitmap</span><span class="o" style="font-weight:bold;">></span> <span class="n">png_bmp</span><span class="p">(</span><span class="k" style="font-weight:bold;">new</span> <span class="n">TBitmap</span><span class="p">(</span><span class="n">src_png</span><span class="p">.</span><span class="n">get</span><span class="p">(),</span> <span class="n">mask_bmp</span><span class="p">.</span><span class="n">get</span><span class="p">()));</span> <span class="n">png_bmp</span><span class="o" style="font-weight:bold;">-></span><span class="n">SaveToFile</span><span class="p">(</span><span class="n">png_savefname</span><span class="p">);</span> <span class="c1" style="color:#999988;font-style:italic;">//Confirm PNG image.Check whether it is masked.</span> <span class="n">std</span><span class="o" style="font-weight:bold;">::</span><span class="n">tr1</span><span class="o" style="font-weight:bold;">::</span><span class="n">shared_ptr</span><span class="o" style="font-weight:bold;"><</span><span class="n">TBitmap</span><span class="o" style="font-weight:bold;">></span> <span class="n">in_png</span><span class="p">(</span><span class="k" style="font-weight:bold;">new</span> <span class="n">TBitmap</span><span class="p">(</span><span class="n">png_savefname</span><span class="p">));</span> <span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">BeginScene</span><span class="p">();</span> <span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">DrawBitmap</span><span class="p">(</span><span class="n">src_png</span><span class="p">.</span><span class="n">get</span><span class="p">(),</span> <span class="n">src_rect</span><span class="p">,</span> <span class="n">rect_helper</span><span class="o" style="font-weight:bold;">::</span><span class="n">pmv</span><span class="o" style="font-weight:bold;"><</span><span class="n">TRectF</span><span class="p">,</span> <span class="kt" style="color:#445588;font-weight:bold;">float</span><span class="o" style="font-weight:bold;">></span><span class="p">(</span><span class="n">src_rect</span><span class="p">,</span> <span class="mi" style="color:#009999;">50</span><span class="p">,</span><span class="mi" style="color:#009999;">70</span><span class="p">),</span> <span class="mi" style="color:#009999;">1</span><span class="p">);</span> <span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">DrawBitmap</span><span class="p">(</span><span class="n">in_png</span><span class="p">.</span><span class="n">get</span><span class="p">(),</span> <span class="n">src_rect</span><span class="p">,</span> <span class="n">rect_helper</span><span class="o" style="font-weight:bold;">::</span><span class="n">pmv</span><span class="o" style="font-weight:bold;"><</span><span class="n">TRectF</span><span class="p">,</span> <span class="kt" style="color:#445588;font-weight:bold;">float</span><span class="o" style="font-weight:bold;">></span><span class="p">(</span><span class="n">src_rect</span><span class="p">,</span><span class="mi" style="color:#009999;">300</span><span class="p">,</span><span class="mi" style="color:#009999;">70</span><span class="p">),</span> <span class="mi" style="color:#009999;">1</span><span class="p">);</span> <span class="n">Canvas</span><span class="o" style="font-weight:bold;">-></span><span class="n">EndScene</span><span class="p">();</span> <span class="p">}</span> |
出力結果
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition