Silverlight缩略图生成(使用WriteableBitmap类)

前段時間,一直因為silverlight生成縮略圖而飽受困擾。

雖然聽說過WriteableBitmap這個類,能生成縮略圖。也曾百度上面粗略看過,一直沒找到合適的生成縮略圖的辦法。

今天,再次試過,終于成功。下面貼出代碼,實際的向大家說明一下,縮略圖是如何生成的。

/// 生成縮略圖
/// </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;
}

 



下面展示一個示例:

复制代码
 1 <UserControl x:Class="SilverlightTest.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6     mc:Ignorable="d"
 7     d:DesignHeight="300" d:DesignWidth="400">
 8 
 9 
10     <Grid x:Name="LayoutRoot" Background="White">
11         <Grid.RowDefinitions>
12             <RowDefinition></RowDefinition>
13             <RowDefinition></RowDefinition>
14         </Grid.RowDefinitions>
15         <Image x:Name="TestImg1" Grid.Row="0" Source="/SilverlightTest;component/Images/20120229_reg_page1_09.jpg" Stretch="None"></Image>
16         <Image x:Name="TestImg"  Grid.Row="1" Source="/SilverlightTest;component/Images/20120229_reg_page1_09.jpg" Stretch="None"></Image>
17         <TextBlock x:Name="txt" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Center"></TextBlock>
18     </Grid>
19 </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高级群 号码:261882616

博主以及同事和你共同探讨感兴趣的话题。

 

 

 



posted @   历史的驱动  阅读(729)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示