簡體   English   中英

剪切特定形狀的圖像 .NET

[英]Clip an image in a specific shape .NET

我的 MVC4 項目中有一個頁面,用戶可以在其中使用文件上傳控件添加其公司徽標。 這些圖像/徽標隨后會顯示在移動應用程序的地圖上。 我們需要裁剪這些圖像,使它們看起來像一面旗幟。

在此處輸入圖片說明

我們只需要取標志框內的圖像部分,其余部分則保留。

  1. 可以使用 C# 中的代碼來完成嗎?
  2. 如果是,如何做到。 請幫我提供一些代碼示例和鏈接。
  3. 我需要在上傳的圖像上顯示一個標志框,以便用戶可以在該框架中調整其圖像,以及它想要在框架中的內容。

請向我推薦一些 API 和代碼示例。

謝謝。

更新:在一些網站,當我們上傳個人資料圖片時,它會在頂部給我們一個框架,我們可以移動我們選擇的圖像,以便所需的部分進入該框架。 現在當我們上傳我們的個人資料圖片時,它會被調整到那個大小。 我可以在這里做類似的事情嗎? 在上面的框架中,我可以給出一個標志形狀,用戶可以移動上傳的圖像,以獲得該框架中所需的圖像部分。 這是正確的方法嗎? 我們應該怎么做? 我查看了一些 jquery 代碼示例,但沒有幫助。

您可以使用 SetClip 函數和 Region 作為參數:

https://msdn.microsoft.com/en-us/library/x1zb278e(v=vs.110).aspx

因此,您需要從 Bitmap 創建 Graphics 對象,使用標志的形狀設置剪輯,然后在該 Graphics 對象上繪制圖像。 就這樣。

  // Following code derives a cutout bitmap using a
  // scizzor path as a clipping region (like Paint would do)
  // Result bitmap has a minimal suitable size, pixels outside 
  // the clipping path will be white.

  public static Bitmap ApplyScizzors(Bitmap bmpSource, List<PointF> pScizzor)
    {
        GraphicsPath graphicsPath = new GraphicsPath();   // specified Graphicspath          
        graphicsPath.AddPolygon(pScizzor.ToArray());      // add the Polygon
        var rectCutout = graphicsPath.GetBounds();        // find rectangular range           
        Matrix m = new Matrix();
        m.Translate(-rectCutout.Left, -rectCutout.Top);   // translate clip to (0,0)
        graphicsPath.Transform(m);
        Bitmap bmpCutout = new Bitmap((int)(rectCutout.Width), (int)(rectCutout.Height));  // target
        Graphics graphicsCutout = Graphics.FromImage(bmpCutout);
        graphicsCutout.Clip = new Region(graphicsPath);
        graphicsCutout.DrawImage(bmpSource, (int)(-rectCutout.Left), (int)(-rectCutout.Top)); // draw
        graphicsPath.Dispose();
        graphicsCutout.Dispose();
        return bmpCutout;
    }

暫無
暫無

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

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