Silverlight缩略图生成(使用WriteableBitmap类)
来源:互联网 发布:淘宝代购怎么开店 编辑:程序博客网 时间:2024/05/28 18:43
前段時間,一直因為silverlight生成縮略圖而飽受困擾。
雖然聽說過WriteableBitmap這個類,能生成縮略圖。也曾百度上面粗略看過,一直沒找到合適的生成縮略圖的辦法。
今天,再次試過,終于成功。下面貼出代碼,實際的向大家說明一下,縮略圖是如何生成的。
/// <summary>/// 生成縮略圖/// </summary>/// <param name="bitmap">要轉換的位圖</param>/// <returns>返回WriteableBitmap</returns>public WriteableBitmap RenderThumbnail(BitmapImage bitmap){Image img = new Image();img.Width = 150;img.Height = 150;img.Source = bitmap;WriteableBitmap Wimg = new WriteableBitmap(img,null);Wimg.Invalidate();return Wimg;}
下面展示一個示例:
<UserControl x:Class="SilverlightTest.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Image x:Name="TestImg1" Grid.Row="0" Source="/SilverlightTest;component/Images/20120229_reg_page1_09.jpg" Stretch="None"></Image> <Image x:Name="TestImg" Grid.Row="1" Source="/SilverlightTest;component/Images/20120229_reg_page1_09.jpg" Stretch="None"></Image> <TextBlock x:Name="txt" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Center"></TextBlock> </Grid></UserControl>
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Windows.Media.Imaging;namespace SilverlightTest{public partial class MainPage : UserControl{public MainPage(){InitializeComponent();BitmapImage bmap = TestImg.Source as BitmapImage;WriteableBitmap wimg = this.RenderThumbnail(bmap);TestImg.Source = wimg;txt.Text ="原圖大小為:" +bmap.PixelWidth + "Px;縮略圖寬度為:" + wimg.PixelWidth+"Px";}/// <summary>/// 生成縮略圖/// </summary>/// <param name="bitmap">要轉換的位圖</param>/// <returns>返回WriteableBitmap</returns>public WriteableBitmap RenderThumbnail(BitmapImage bitmap){Image img = new Image();img.Width = 150;img.Height = 150;img.Source = bitmap;WriteableBitmap Wimg = new WriteableBitmap(img,null);Wimg.Invalidate();return Wimg;}}}
請看如下效果:
更多WEB開發技術請加群:Asp.Net/Wp8高級開發群 號碼:261882616
博主以及同事和你共同探討感興趣的話題。
- Silverlight缩略图生成(使用WriteableBitmap类)
- Silverlight之WriteableBitmap的使用
- Silverlight 3.0 中的 WriteableBitmap
- Silverlight 3.0 中的 WriteableBitmap
- windows phone WriteableBitmap类的使用
- 使用AVFoundation生成缩略图
- 使用WriteableBitmap进行截图
- WriteableBitmap 的使用
- Silverlight中实现WriteableBitmap转为Byte流
- Silverlight - WriteableBitmap的跨域调用限制
- WriteableBitmap
- 图片生成缩略图类
- PHP 生成缩略图类
- php生成缩略图类
- 缩略图生成类
- PHP生成缩略图类
- 使用404被动生成缩略图
- 使用七牛云存储生成缩略图
- 如何在iOS中使用Block──How to use Blocks with iOS
- Nginx+PHP的虚拟主机目录权限控制
- IIS6和IIS7下都可以用的伪静态工具ISAPI_Rewrite3的使用说明
- 32位系统中四字节对齐问题
- 创业者该知道的10件事
- Silverlight缩略图生成(使用WriteableBitmap类)
- Access restriction: Class is not accessible due to restriction on required library 错误
- 结构体,联合体,枚举体这三种数据结构的感想
- 虚函数、纯虚函数详解
- SVN与Git比较
- 程序员的十大谎言
- AIR-开机自启动
- 图像处理基础理论-颜色
- 谷歌开始支持CardDAV协议与iOS同步联系人信息