当前位置: 首页 > news >正文

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

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

 

 

 



转载于:https://www.cnblogs.com/lilin123/archive/2012/10/31/2747589.html

相关文章:

  • 图像处理滤波应用(Halcon)
  • SQL快速查找
  • 21-Heartbeat配置文件authkey重要参数讲解
  • 手机不可以上网
  • springmvc+myabtis+dubbo分布式平台-maven构建根项目
  • 打印水仙花数(汇编)
  • 【Perl】批量word和PPT文档转pdf
  • mysql 5.7.20解压版安装配置
  • coredump简介与coredump原因总结
  • 使用paramiko模块批量修改Linux主机的密码
  • 《经济学人 —— 人工智能专题报告》
  • 五大常用算法简述
  • 如何用数学课件制作工具画角平分线
  • 自己define size_t编译出错
  • sed
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【React系列】如何构建React应用程序
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • 230. Kth Smallest Element in a BST
  • 30天自制操作系统-2
  • ES6 学习笔记(一)let,const和解构赋值
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • HTTP--网络协议分层,http历史(二)
  • JAVA 学习IO流
  • MaxCompute访问TableStore(OTS) 数据
  • node学习系列之简单文件上传
  • PHP 7 修改了什么呢 -- 2
  • Selenium实战教程系列(二)---元素定位
  • v-if和v-for连用出现的问题
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 算法-插入排序
  • 微信小程序开发问题汇总
  • 我看到的前端
  • 新书推荐|Windows黑客编程技术详解
  • 一些css基础学习笔记
  • 源码安装memcached和php memcache扩展
  • 最近的计划
  • 浅谈sql中的in与not in,exists与not exists的区别
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #AngularJS#$sce.trustAsResourceUrl
  • #宝哥教你#查看jquery绑定的事件函数
  • $(function(){})与(function($){....})(jQuery)的区别
  • (16)Reactor的测试——响应式Spring的道法术器
  • (AngularJS)Angular 控制器之间通信初探
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (分类)KNN算法- 参数调优
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (蓝桥杯每日一题)love
  • (转)IOS中获取各种文件的目录路径的方法
  • (转)可以带来幸福的一本书
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树