winPhone开启摄像头
前台代码:
<!--LayoutRoot 是包含所有页面内容的根网格--> <Grid x:Name="LayoutRoot" Background="#eee"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid x:Name="ContentPanel" Margin="12,0,12,578" Grid.Row="1">
<ListBox Height="768" Margin="-12,0,-12,-578" Name="listBox1" Opacity="8" VerticalAlignment="Center">
<ListBox.ItemTemplate> <DataTemplate> <Image Width="500" Height="500" Margin="0,5,0,5" Source="{Binding}"></Image> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
</Grid>
<!--演示 ApplicationBar 用法的示例代码--> <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> <shell:ApplicationBarIconButton IconUri="appbar.feature.camera.rest.png" Text="按钮 1" Click="ApplicationBarIconButton_Click" />
</shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>
后台代码:
private ObservableCollection<BitmapImage> bitmapImages = new ObservableCollection<BitmapImage>();
public Page4()
{
InitializeComponent();
LoadImg();
listBox1.ItemsSource = bitmapImages;
}
private void LoadImg()
{
bitmapImages.Clear();
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
try
{
foreach (string file in isf.GetFileNames("baby"+"*.jpg"))//遍历所有jpg图片
{
BitmapImage bmpImg = new BitmapImage();
using (Stream stream = isf.OpenFile(file, FileMode.Open))
{
bmpImg.SetSource(stream);
}
bitmapImages.Add(bmpImg);
}
}
catch { }
}
void pcTask_Completed(object sender, PhotoResult e)
{
if (e.Error != null)
{
CommonHelper.MsgBox("执行失败!");
return;
}
if (e.TaskResult == TaskResult.OK)
{
//BitmapImage img = new BitmapImage();
//img.SetSource(e.ChosenPhoto);//用图片流初始化BitmapImage图片对象
string name = (string)IsolatedStorageSettings.ApplicationSettings["name"];
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
string filename = "baby"+name+DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
using (e.ChosenPhoto)
{
Stream stream = isf.CreateFile(filename);
e.ChosenPhoto.CopyTo(stream);
BitmapImage bmpImg = new BitmapImage();
bmpImg.SetSource(stream);
bitmapImages.Add(bmpImg);
}
}
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
PhotoChooserTask pcTask = new PhotoChooserTask();
pcTask.ShowCamera = true;//显示拍照按钮,用户即可以选择图片,又可以拍照
pcTask.PixelHeight = 100;//如果设定了PixelHeight和PixelWidth,则选择后的图片要求用户截取成合适的大小!
pcTask.PixelWidth = 100;//可以用来实现上传QQ头像的功能。
pcTask.Completed += new EventHandler<PhotoResult>(pcTask_Completed);
pcTask.Show();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构