wp8 入门到精通 ImageCompress 图片压缩

//实例化选择器
        PhotoChooserTask photoChooserTask = new PhotoChooserTask();
        BitmapImage bimg;
        int newPixelWidth;
        int newPixelHeight;    

//构造函数里

            //从页面读取输入值作为目标分辨率
            newPixelHeight = int.Parse(textBox2.Text.ToString());
                       //从页面读取输入值作为目标分辨率
            newPixelHeight = int.Parse(textBox2.Text.ToString());
            newPixelWidth = int.Parse(textBox1.Text.ToString());

            textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
            textBox2.TextChanged += new TextChangedEventHandler(textBox2_TextChanged);

        void textBox2_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBox2.Text != "" && int.Parse(textBox2.Text.ToString()) != 0)
            {
                newPixelHeight = int.Parse(textBox2.Text.ToString());
            }
        }

        void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBox1.Text != "" && int.Parse(textBox1.Text.ToString()) != 0)
            {
                 newPixelWidth = int.Parse(textBox1.Text.ToString());
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //为JPEG文件创建一个流
            StreamResourceInfo sri = null;

            //将Tulips.jpg的Build Action设为Content
            Uri jpegUri = new Uri("Images/Tulips.jpg", UriKind.Relative);
            sri = Application.GetResourceStream(jpegUri);

            //将该流解码
            WriteableBitmap bitmap = PictureDecoder.DecodeJpeg(sri.Stream);
            img.Source = bitmap;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            String tempjpg = "tempimg";

            //创建虚拟存储
            var myStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (myStore.FileExists(tempjpg))
            {
                myStore.DeleteFile(tempjpg);
            }

            IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempjpg);
            StreamResourceInfo sri = null;
            sri = Application.GetResourceStream(new Uri("Images/Tulips.jpg", UriKind.Relative));

            //创建WriteableBitmap对象,
            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.SetSource(sri.Stream);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            //WriteableBitmap对象编码,设置目标分辨率
            wb.SaveJpeg(myFileStream, newPixelWidth, newPixelHeight, 0, 85);
            myFileStream.Close();

            myFileStream = myStore.OpenFile(tempjpg, FileMode.Open, FileAccess.Read);

            //保存图片到图片库
            MediaLibrary library = new MediaLibrary();
            Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);

            myFileStream.Close();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //打开图片库
            photoChooserTask.Show();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

        }

        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                bimg = new BitmapImage();
                bimg.SetSource(e.ChosenPhoto);
                //设置Image控件的数据源
                img.Source = bimg;
            }
        }

UI

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Image x:Name="img" Height="300"  VerticalAlignment="Top" />
            <Button Content="加载图片" Height="82" HorizontalAlignment="Left" Margin="150,0,0,219" Name="button1" VerticalAlignment="Bottom" Width="160" Click="button1_Click" />
            <Button Content="保存图片" Height="72" HorizontalAlignment="Left" Margin="150,376,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
            <Button Content="查看图片" Height="72" HorizontalAlignment="Left" Margin="150,438,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="150,535,0,0" Name="textBox1" Text="20" VerticalAlignment="Top" Width="114" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="251,535,0,0" Name="textBox2" Text="20" VerticalAlignment="Top" Width="116" />
            <TextBlock Height="46" HorizontalAlignment="Left" Margin="-12,555,0,0" Name="textBlock1" Text="设置目标分辨率" VerticalAlignment="Top" Width="156" />
        </Grid>
    </Grid>
时间: 2025-01-02 05:30:09

wp8 入门到精通 ImageCompress 图片压缩的相关文章

wp8 入门到精通 测量代码执行时间

Stopwatch time = new Stopwatch(); byte[] target = new byte[size]; for (int j = 0; j < size; j++) target[j] = unchecked((byte)j); //Otherwise parts of the array are optimised out. CCMD5Core.GetHash(target); time.Start(); for (int i = 1; i <= iteratio

wp8 入门到精通 输入框

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Text="Default" Height="30" HorizontalAlignment="Left" Margin="10,15,0,0" Name="textDefault" Ve

wp8 入门到精通 Utilities类 本地存储+异步

public class CCSetting { public async static void AddOrUpdateValue<T>(string key, T value) { try { if (key != null) { StorageFolder floder = ApplicationData.Current.LocalFolder; if (!(await floder.GetFoldersAsync()).Any(a => a.Name == "DrieS

wp8 入门到精通 高仿微信发信息 键盘不消失

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel Orientation="Vertical"> <TextBlock Text="Hides soft keyboard on enter:" /> <TextBox x:Name="textBox"

wp8 入门到精通 启动系统分享照片任务

PhotoChooserTask photoChooserTask = new PhotoChooserTask(); photoChooserTask.Completed += photoChooserTask_Completed; photoChooserTask.ShowCamera = true; photoChooserTask.Show(); void photoChooserTask_Completed(object sender, PhotoResult e) { if (e.T

wp8 入门到精通 ---转换

/// <summary>        /// 颜色字符串转Color        /// </summary>        public static Color ConvertFromString(string argb)        {            uint result;            if (uint.TryParse(argb.TrimStart('#', '0'), NumberStyles.HexNumber, null, out resu

wp8 入门到精通 Gallery

<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.Resources> <Style TargetType="Image"> <Setter Property="Margin" Value="-12,-36,0,0"/> </Style> </Grid.Resources>

wp8 入门到精通 MultiMsgPrompt

List<NotifyMsg> arraymsg = new List<NotifyMsg>(); List<NotifyInfo> ArrayNotifyInfo = new List<NotifyInfo>() { new NotifyInfo(){title="[收信宝]开通失败",content="您的[收信宝]服务未能开通,请重试或联系客服确认订单.{失败原因:失败原因,新快捷支付:支付失败,请重新支付,或联系客服确认

wp8 入门到精通 定时更新瓷贴

public class ScheduledAgent : ScheduledTaskAgent { static ScheduledAgent() { Deployment.Current.Dispatcher.BeginInvoke(delegate { Application.Current.UnhandledException += UnhandledException; }); } private static void UnhandledException(object sender