JpegBitmapDecoder doesn't work on some .jpg files

I have a jpeg file that I fails when I attempt to get a JpegBitmap

Page 1 of 1

1 Replies - 5835 Views - Last Post: 26 February 2009 - 02:21 PM Rate Topic: -----

#1 StCroixSkipper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 121
  • Joined: 23-December 08

JpegBitmapDecoder doesn't work on some .jpg files

Posted 26 February 2009 - 01:09 PM

I'm using a WPF Window to display a .jpg file. The following code works fine. If I remove the backslashes on the "//BitmapDecoder decoder = new JpegBitmapDecoder(uri,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.onload);" line, I get and exception: {"The codec cannot use the type of URI provided."}

Any ideas why I'm getting this exception? I don't get it on every file. Just a few of my .jpg files.

I might be able to attach one of the offending jpg's. But I'll post this first.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

namespace xxx
{
	public class ImageButton : Window
	{
		[STAThread]
		public static void Main()
		{
			Application app = new Application();
			app.Run(new ImageButton());
		}
		public ImageButton()
		{
			Title = "ImageButton";

			string path = "C:\\nextBtn.jpg";
			if (File.Exists(path))
			{
				Uri uri = new Uri(path);
				//BitmapDecoder decoder = new JpegBitmapDecoder(uri, 
					BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.onload);

				BitmapImage bitmap = new BitmapImage(uri);

				Image img = new Image();
				img.Opacity = 50;
				img.Source = bitmap;
				img.Stretch = Stretch.None;

				Button btn = new Button();
				btn.Content = img;
				btn.HorizontalAlignment = HorizontalAlignment.Center;
				btn.VerticalAlignment = VerticalAlignment.Center;
				Content = btn;
			}
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: JpegBitmapDecoder doesn't work on some .jpg files

#2 StCroixSkipper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 121
  • Joined: 23-December 08

Re: JpegBitmapDecoder doesn't work on some .jpg files

Posted 26 February 2009 - 02:21 PM

I tried to edit the jpeg file in GIMP and save it to a new jpg file and low and behold the JpegBitmapDecoder() statment worked fine.

This isn't the only jpg file I have this problem with. Something must be wrong with these files.

View PostStCroixSkipper, on 26 Feb, 2009 - 12:09 PM, said:

I'm using a WPF Window to display a .jpg file. The following code works fine. If I remove the backslashes on the "//BitmapDecoder decoder = new JpegBitmapDecoder(uri,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.onload);" line, I get and exception: {"The codec cannot use the type of URI provided."}

Any ideas why I'm getting this exception? I don't get it on every file. Just a few of my .jpg files.

I might be able to attach one of the offending jpg's. But I'll post this first.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

namespace xxx
{
	public class ImageButton : Window
	{
		[STAThread]
		public static void Main()
		{
			Application app = new Application();
			app.Run(new ImageButton());
		}
		public ImageButton()
		{
			Title = "ImageButton";

			string path = "C:\\nextBtn.jpg";
			if (File.Exists(path))
			{
				Uri uri = new Uri(path);
				//BitmapDecoder decoder = new JpegBitmapDecoder(uri, 
					BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.onload);

				BitmapImage bitmap = new BitmapImage(uri);

				Image img = new Image();
				img.Opacity = 50;
				img.Source = bitmap;
				img.Stretch = Stretch.None;

				Button btn = new Button();
				btn.Content = img;
				btn.HorizontalAlignment = HorizontalAlignment.Center;
				btn.VerticalAlignment = VerticalAlignment.Center;
				Content = btn;
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1