- [圓形頭像](#)
- [去掉黑邊](#)
- [拖動打開圖形](#)
## 圓形頭像
現在很多軟件都喜歡使用圓形頭像
win10 uwp使用圓形頭像很簡單
~~~
<Ellipse Width="200" Height="200" Margin="10,10,10,10">
<Ellipse.Fill>
<ImageBrush ImageSource="assets/1.jpg"/>
</Ellipse.Fill>
</Ellipse>
~~~
使用這樣的圓形頭像沒有對原有圖形的渲染大小進行變化,一個大的圖形不會解碼為剛好要的,我們進行一步修改
代碼:
~~~
<Page
x:Class="Roundhead.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Roundhead"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Vertical">
<Ellipse Width="200" Height="200" Margin="10,10,10,10">
<Ellipse.Fill>
<ImageBrush ImageSource="assets\1.jpg"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="我的頭像是圓" HorizontalAlignment="Center" />
</StackPanel>
</Grid>
</Page>
~~~

## 去掉黑邊
程序界面有一些

看起來不好
在app.xaml.cs找到`this.DebugSettings.EnableFrameRateCounter = true;`
寫為`false`

## 拖動打開圖形
把`<ImageBrush ImageSource="assets\1.jpg"/>`添加`x:Name="ximg"`
在Grid增加`AllowDrop="True" DragOver="Grid_DragOver" Drop="Grid_Drop"`
在`Grid_Drop`
~~~
private async void Grid_Drop(object sender , DragEventArgs e)
{
var defer = e.GetDeferral();
try
{
DataPackageView dataView = e.DataView;
// 拖放類型為文件存儲。
if (dataView.Contains(StandardDataFormats.StorageItems))
{
var files = await dataView.GetStorageItemsAsync();
StorageFile file = files.OfType<StorageFile>().First();
if (file.FileType == ".png" || file.FileType == ".jpg")
{
// 拖放的是圖片文件。
BitmapImage bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenAsync(FileAccessMode.Read));
ximg.ImageSource = bitmap;
}
}
}
finally
{
defer.Complete();
}
}
~~~
在`Grid_DragOver`
~~~
private void Grid_DragOver(object sender , DragEventArgs e)
{
//需要using Windows.ApplicationModel.DataTransfer;
e.AcceptedOperation = DataPackageOperation.Copy;
// 設置拖放時顯示的文字。
//e.DragUIOverride.Caption = "拖放打開";
// 是否顯示拖放時的文字。默認為 true。
//e.DragUIOverride.IsCaptionVisible = false;
// 是否顯示文件預覽內容,一般為文件圖標。默認為 true。
// e.DragUIOverride.IsContentVisible = false;
// Caption 前面的圖標是否顯示。默認為 true。
//e.DragUIOverride.IsGlyphVisible = false;
//需要using Windows.UI.Xaml.Media.Imaging;
//設置拖動圖形,覆蓋文件預覽
//e.DragUIOverride.SetContentFromBitmapImage(new BitmapImage(new Uri("ms-appx:///Assets/1.jpg")));
e.Handled = true;
}
~~~

`e.AcceptedOperation = DataPackageOperation.Copy;`設置拖動作為復制
需要`using Windows.ApplicationModel.DataTransfer`
拖放顯示文字`e.DragUIOverride.Caption = "拖放打開";`

是否顯示拖放時的文字。默認為 true`e.DragUIOverride.IsCaptionVisible = false;`

復制圖標是否顯示 `e.DragUIOverride.IsGlyphVisible = false;`

設置拖動圖形,覆蓋文件預覽`e.DragUIOverride.SetContentFromBitmapImage(new BitmapImage(new Uri(img)));`

代碼:[https://code.csdn.net/lindexi_gd/lindexi_gd/tree/master/Roundhead](https://code.csdn.net/lindexi_gd/lindexi_gd/tree/master/Roundhead)
參考:[http://timheuer.com/blog/archive/2015/05/06/making-circular-images-in-xaml-easily.aspx](http://timheuer.com/blog/archive/2015/05/06/making-circular-images-in-xaml-easily.aspx)
- 前言
- UWP win10 app 新關鍵字x:Bing
- win10應用 UWP 使用MD5算法
- win10 UWP讀寫文件
- UWP appButtonBar樣式
- C# 6.0 $&quot;Hello {csdn}&quot;
- Win10 UWP xaml 延遲加載元素
- UWP xaml 圓形頭像
- UWP 繪制圖形
- win10 uwp 通知Toast
- win10 UWP 顯示地圖
- win10 uwp 參考
- win10 uwp clone
- win10 uwp 裝機必備應用 含源代碼
- RichEditBox 使用自定義菜單
- win10 UWP FlipView
- win10 UWP 獲取系統信息
- win10 UWP 申請微軟開發者
- win10 UWP button
- win10 UWP Markdown 含源代碼
- win10 UWP 應用設置
- win10 UWP 九幽數據分析
- win10 UWP 圓形等待
- win10 UWP 標題欄后退
- win10 UWP 單元測試
- win10 UWP 你寫我讀
- win10 UWP RSS閱讀器
- win10 UWP MessageDialog 和 ContentDialog
- win10 UWP Hmac
- win10 UWP GET Post
- Win10 UWP Intro to controls and events
- win10 UWP Controls by function
- win10 uwp App-to-app communication 應用通信