CSharp

半透明の画像を表示する方法

方法一:

//PictureBox1のGraphicsオブジェクトを取得
Graphics g = PictureBox1.CreateGraphics(); 

//画像を読み込む
Image img = Image.FromFile(@"C:\サンプル.jpg"); 

//ColorMatrixオブジェクトの作成
System.Drawing.Imaging.ColorMatrix cm =
   new System.Drawing.Imaging.ColorMatrix();
//ColorMatrixの行列の値を変更して、アルファ値が0.5に変更されるようにする
cm.Matrix00 = 1;
cm.Matrix11 = 1;
cm.Matrix22 = 1;
cm.Matrix33 = 0.5F;
cm.Matrix44 = 1;

//ImageAttributesオブジェクトの作成
System.Drawing.Imaging.ImageAttributes ia =
   new System.Drawing.Imaging.ImageAttributes();
//ColorMatrixを設定する
ia.SetColorMatrix(cm);

//ImageAttributesを使用して画像を描画
g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height),
   0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);

//リソースを開放する
img.Dispose();
g.Dispose();

方法二:

       /// <summary>
       /// アルファブレンドした画像を描写する
       /// </summary>
       /// <param name="img">ブレンドする画像。背景bgImageはgetBackGroundImage()で取得済</param>
       /// <param name="alpha">透過値。0~1の間</param>
       private void DrawBrendImage(Image img, float alpha)
       {
           //ブレンドして表示する背景を準備
           Bitmap brendBmp = (Bitmap)bgImage.Clone();
           Graphics brendG = Graphics.FromImage(brendBmp);

           //System.Drawing.Imaging.ColorMatrixオブジェクトの作成
           //ColorMatrixの行列の値を変更して、アルファ値がalphaに変更されるようにする
           ColorMatrix cm = new ColorMatrix();
           cm.Matrix00 = 1;
           cm.Matrix11 = 1;
           cm.Matrix22 = 1;
           cm.Matrix33 = alpha;
           cm.Matrix44 = 1;

           //System.Drawing.Imaging.ImageAttributesオブジェクトの作成
           //ColorMatrixを設定、ImageAttributesを使用して背景に描画
           ImageAttributes ia = new ImageAttributes();
           ia.SetColorMatrix(cm);

           //アルファブレンドしながら描写
           brendG.DrawImage(
               img,
               new Rectangle(0, 0, img.Width, img.Height),
               0, 0,
               img.Width, img.Height,
               GraphicsUnit.Pixel, ia);

           //合成された画像を表示
           using (Graphics g = this.CreateGraphics())
           {
               g.DrawImage(brendBmp, 0, 0);
           }
           //リソースを開放する
           brendG.Dispose();
           brendBmp.Dispose();
       }
 
       /// <summary>
       /// 背景となる画像を取得する。
       /// 取得した画像はbgImageに保存。
       /// </summary>
       private void getBackGroundImage()
       {
           //表示位置の背景を取得
           Rectangle rc;
           rc = this.RectangleToScreen(new Rectangle(0, 0, this.Width, this.Height));

           bgImage = new Bitmap(
             rc.Width,
             rc.Height,
             PixelFormat.Format32bppArgb
             );
           using (Graphics gBgImage = Graphics.FromImage(bgImage))
           {
               gBgImage.CopyFromScreen(
                   rc.X, rc.Y,
                   0, 0,
                   rc.Size,
                   CopyPixelOperation.SourceCopy);
           }
       }


(画像の文字列を入力して下さい)

トップ   編集 凍結 差分 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019/12/02 (月) 12:45:40 (1601d)

PCpዾyǗlgĂ܂}WŔ܂z 萔O~ył񂫁z Yahoo yV NTT-X Store

z[y[W ̃NWbgJ[h COiq ӂ邳Ɣ[ COsیI COze