I'm having issues opening multiple image files from the users desktop and then converting those images to a scaled down size which then gets displayed on the UI (after all the converting is done). I can't find what the issue is exactly but what I've observed is that there seems to be a 5 second limit between hitting the "Open" button on the "OpenFileDialog" box control and how much time I have to read those File(s). I've used 6 files ranging in size of 9-11MB, and in another case I've used 50 1-2MB files and in all cases the process will read up until 5 seconds have expired. It never fails on the same image either so the image isn't causing the issue which would further make me believe its not a file count issue. If I test this process with only a few small sized files it happens under 1 second and there is not failure and I see all images on the UI. That is why I'm guessing its a timing issue. I know silverlight has a security exception between when the user interacts with a control (button) and how much time can elapse before displaying the "OpenFileDialog" box but this time limit seems to be different but I can't find any documentation.
Here is the code I'm using. It seems to be a pretty common recipe used everywhere but posting for completeness. The error happens on the line
var bitmap = new WriteableBitmap(bitmapImage);
The reason it fails is because the bitmapImage pixelWidth/Height == 0. Here is the full code.
private const int MaxPixelSize = 500;
public byte[] Convert(FileInfo fileInfo, FileTypes fileType, DateTime startTime)
{
byte[] result = null;
using (var stream = fileInfo.OpenRead())
{
DateTime EndTime = DateTime.Now;
if (fileType == FileTypes.JPG || fileType == FileTypes.BMP || fileType == FileTypes.PNG)
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
double scaleX = 1;
double scaleY = 1;
if (bitmapImage.PixelWidth > MaxPixelSize)
{
scaleX = MaxPixelSize / (double)bitmapImage.PixelWidth;
}
if (bitmapImage.PixelHeight > MaxPixelSize)
{
scaleY = MaxPixelSize / (double)bitmapImage.PixelHeight;
}
var scale = Math.Min(scaleX, scaleY);
var bitmap = new WriteableBitmap(bitmapImage);
var resizedBitmap = bitmap.Resize((int)((double)bitmapImage.PixelWidth * scale), (int)((double)bitmapImage.PixelHeight * scale), WriteableBitmapExtensions.Interpolation.Bilinear);
using (var scaleStream = new MemoryStream())
{
var encoder = new JpegEncoder();
var image = resizedBitmap.ToImage();
encoder.Encode(image, scaleStream);
result = scaleStream.GetBuffer();
}
}
else
{
result = new byte[stream.Length];
stream.Read(result, 0, (int)stream.Length);
}
}
return result;
}
Any help or suggestions are welcomed.
Thanks,
Dean
if bitmapImage.ImageOpened event is executed, you can get valid pixelWidth and height.
when bitmapImage.SetSource(stream) is excuted, this event will be invoked.
Related
I'm using ImageProcessor to reduce the resolution or quality of an image, but I'm don't know how to make sure that the image resultant size it's below 5 megabytes. I tried setting the image dimensions to 3840-2160 but I want to use a better option.
Here it's my code:
private static byte[] redimensionImage(ref byte[] photoBytes)
{
var byteCuantity = ConvertBytesToMegabytes(photoBytes.Count());
ISupportedImageFormat format = new JpegFormat();
using (MemoryStream inStream = new MemoryStream(photoBytes))
{
using (MemoryStream outStream = new MemoryStream())
{
// Initialize the ImageFactory using the overload to preserve EXIF metadata.
using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
{
// Load, resize, set the format and quality and save an image.
using (var imageProcessor = imageFactory.Load(inStream))
{
var originalHeight = imageProcessor.Image.Size.Height;
var originalWidth = imageProcessor.Image.Size.Width;
//calculate aspect ratio
var aspect = originalWidth / (float)originalHeight;
int newWidth, newHeight;
var dimenssionTooSmall = false;
if (originalWidth <= originalHeight && originalWidth < 100)
{
//calculate new dimensions based on aspect ratio
newHeight = (int)(100 / aspect);
var resizeLayer = new ResizeLayer(new Size(100, newHeight), ResizeMode.Min);
imageProcessor.Resize(resizeLayer);
dimenssionTooSmall = true;
}
else if (originalHeight < originalWidth && originalHeight < 100)
{
//calculate new dimensions based on aspect ratio
newWidth = (int)(100 / aspect);
var resizeLayer = new ResizeLayer(new Size(newWidth, 100), ResizeMode.Min);
imageProcessor.Resize(resizeLayer);
dimenssionTooSmall = true;
}
if (byteCuantity > 1 || dimenssionTooSmall)
{
//format.Quality = 6;
imageProcessor.Resize(new ResizeLayer(new Size(3840, 2160), ResizeMode.Min));
imageProcessor.Format(format);
imageProcessor.Save(outStream);
return outStream.ToArray();
}
else
{
return inStream.ToArray();
}
}
}
}
}
}
Thanks and regards.
Unfortunately there's no way you can really do this without reprocessing unless you're saving as bitmap.
When you save an image there are many compression processes that take place to store the image in each individual format (except bitmap which doesn't compress the image). Without actually going through the process itself you can't predict the file size.
You could potentially create your own lookup tables to act as a guideline by resizing a large sample of images in different formats and collecting the output sizes to give you a rough estimate for future processing.
#functions{
public void GetThumbnailView(string originalImagePath, int height, int width)
{
//Consider Image is stored at path like "ProductImage\\Product1.jpg"
//Now we have created one another folder ProductThumbnail to store thumbnail image of product.
//So let name of image be same, just change the FolderName while storing image.
string thumbnailImagePath = originalImagePath;
originalImagePath = originalImagePath.Replace("thumb_", "");
//If thumbnail Image is not available, generate it.
if (!System.IO.File.Exists(Server.MapPath(thumbnailImagePath)))
{
System.Drawing.Image imThumbnailImage;
System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath));
double originalWidth = OriginalImage.Width;
double originalHeight = OriginalImage.Height;
double ratioX = (double)width / (double)originalWidth;
double ratioY = (double)height / (double)originalHeight;
double ratio = ratioX < ratioY ? ratioX : ratioY; // use whichever multiplier is smaller
// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
imThumbnailImage = OriginalImage.GetThumbnailImage(newWidth, newHeight,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
imThumbnailImage.Save(Server.MapPath(thumbnailImagePath), System.Drawing.Imaging.ImageFormat.Jpeg);
imThumbnailImage.Dispose();
OriginalImage.Dispose();
}
}
public bool ThumbnailCallback() { return false; }
}
in another stackowerflow question i found this code and really liked it but while using it there was a problem occured while creating the thumbnail images as shown below:
Server Error in '/' Application.
Out of memory. Description: An unhandled exception occurred during
the execution of the current web request. Please review the stack
trace for more information about the error and where it originated in
the code.
Exception Details: System.OutOfMemoryException: Out of memory.
Source Error:
Line 199: {
Line 200: System.Drawing.Image imThumbnailImage;
Line 201: System.Drawing.Image OriginalImage =
System.Drawing.Image.FromFile(Server.MapPath(originalImagePath.ToString()));
Line 202:
Line 203: double originalWidth = OriginalImage.Width;
Source File: c:\Inetpub\wwwroot\Lokal\Views\Stok\SatisRaporu.cshtml
Line: 201
my curiosity about this issue got me into the exception details and seen this :
//
// Summary:
// Creates an System.Drawing.Image from the specified file.
//
// Parameters:
// filename:
// A string that contains the name of the file from which to create the System.Drawing.Image.
//
// Returns:
// The System.Drawing.Image this method creates.
//
// Exceptions:
// System.OutOfMemoryException:
// The file does not have a valid image format.-or- GDI+ does not support the
// pixel format of the file.
//
// System.IO.FileNotFoundException:
// The specified file does not exist.
//
// System.ArgumentException:
// filename is a System.Uri.
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static Image FromFile(string filename);
but all my pictures in that folder has ".jpg" extention so it seems wierd to me.
if im not gonna be able to create thumbnails from ".jpg" what else i can do?
I actually want to learn about if anyone else tried this on ".jpg" files and got a problem with it? and If no problem occured what i might be doing wrong?
A little note: I do this in a view using razor syntax. I know a little about c# language and improving my knowledge about it everyday.
Edit :
How i call the function:
GetThumbnailView("../pics/thumb_" + (("0000000" + stocks.stockcode).Substring(("0000000" + stocks.stockcode).Length - 7, 7)) + ".jpg", 200, 200);
A website I work on generates its thumbnails using the WPF APIs instead of GDI+. You need to add two references to your project to enable this: WindowsBase, PresentationFramework and PresentationCore. Here’s a basic example of how the code might be used:
try
{
using (var input = File.Open(inputFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var thumb = File.Open(thumbFilename, FileMode.Create, FileAccess.Write, FileShare.None))
{
Thumbnail(input, thumb, 200, 100);
}
}
catch (MyException)
{
File.Delete(thumbFilename);
}
This fits the thumbnail into a 200x100 rectangle, while preserving aspect ratio.
(The real website doesn’t do it quite like the above. What we actually do is attempt to generate the smallest thumbnail in the file upload POST handler. We use a memory stream to hold the resulting thumbnail. If the thumbnail could be generated correctly, we save the upload and the small thumbnail, otherwise we return an error response to the client. Other thumbnail sizes are generated on the fly and cached.)
Here’s the code - note that I may have messed up a bit while transforming this into something reusable, but the core bits should all be there. Note that it saves all thumbnails as JPEG, but allows multiple input formats, including JPEG and PNG. This might or might not be OK for you.
private static void Thumbnail(Stream source, Stream destination, int maxWidth, int maxHeight)
{
int width = 0, height = 0;
BitmapFrame frame = null;
try
{
frame = BitmapDecoder.Create(source, BitmapCreateOptions.None, BitmapCacheOption.None).Frames[0];
width = frame.PixelWidth;
height = frame.PixelHeight;
}
catch
{
throw new MyException("The image file is not in any of the supported image formats.");
}
if (width > AbsoluteLargestUploadWidth || height > AbsoluteLargestUploadHeight)
throw new MyException("This image is too large");
try
{
int targetWidth, targetHeight;
ResizeWithAspect(width, height, maxWidth, maxHeight, out targetWidth, out targetHeight);
BitmapFrame targetFrame;
if (frame.PixelWidth == targetWidth && frame.PixelHeight == targetHeight)
targetFrame = frame;
else
{
var group = new DrawingGroup();
RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.HighQuality);
group.Children.Add(new ImageDrawing(frame, new Rect(0, 0, targetWidth, targetHeight)));
var targetVisual = new DrawingVisual();
var targetContext = targetVisual.RenderOpen();
targetContext.DrawDrawing(group);
var target = new RenderTargetBitmap(targetWidth, targetHeight, 96, 96, PixelFormats.Default);
targetContext.Close();
target.Render(targetVisual);
targetFrame = BitmapFrame.Create(target);
}
var enc = new JpegBitmapEncoder();
enc.Frames.Add(targetFrame);
enc.QualityLevel = 80;
enc.Save(destination);
}
catch
{
throw new MyException("The image file appears to be corrupt.");
}
}
/// <summary>Generic helper to compute width/height that fit into specified maxima while preserving aspect ratio.</summary>
public static void ResizeWithAspect(int origWidth, int origHeight, int maxWidth, int maxHeight, out int sizedWidth, out int sizedHeight)
{
if (origWidth < maxWidth && origHeight < maxHeight)
{
sizedWidth = origWidth;
sizedHeight = origHeight;
return;
}
sizedWidth = maxWidth;
sizedHeight = (int) ((double) origHeight / origWidth * sizedWidth + 0.5);
if (sizedHeight > maxHeight)
{
sizedHeight = maxHeight;
sizedWidth = (int) ((double) origWidth / origHeight * sizedHeight + 0.5);
}
}
The file extension doesn't really matter, it is the actual bytes of the image that matter. Most likely one of the jpgs is corrupt. You should catch the OutOfMemory exception on a per file basis and handle that appropriately.
Since you are trying to generate thumbnails, I suggest you have a default image to use if the thumbnail can't be generated. For example, most web browsers use a small box with a red X in it when the image is corrupt or missing.
See also:
SO#6506089
SO#1108607
SO#1644108
SO#9237457
And for those curious about why OutOfMemoryException is thrown, see the answer to this question:
Is there a reason Image.FromFile throws an OutOfMemoryException for an invalid image format?
I have a Kinect WPF Application that takes images from the Kinect, does some feature detection using EmguCV (A C# opencv wrapper) and displays the output on the using a WPF image.
I have had this working before, but the application now refuses to update the screen image when the imagesource is written to, but I have not changed the way it works.
the Image(called video) is written to as such:
video.Source = bitmapsource;
in the colorframeready event handler.
This works fine until I introduce some opencv code before the imagesource is written to. It does not matter what source is used, so I don't think it is a conflict there. I have narrowed down the offending EmguCV code to this line:
RecentKeyPoints = surfCPU.DetectKeyPointsRaw(ImageRecent, null);
which jumps straight into the opencv code. It is worth noting that:
ImageRecent has completely different origins to the bitmapsource updating the screen.
Reading video.Source returns the bitmapsource, so it seems to be writing correctly, just not updating the screen.
Let me know if you want any more information...
void nui_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
// Checks for a recent Depth Image
if (!TrackingReady) return;
// Stores image
using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame())
{
if (colorImageFrame != null)
{
if (FeatureTracker.ColourImageRecent == null)
//allocate the first time
FeatureTracker.ColourImageRecent = new byte[colorImageFrame.PixelDataLength];
colorImageFrame.CopyPixelDataTo(FeatureTracker.ColourImageRecent);
}
else return;
}
FeatureTracker.FeatureDetect(nui);
//video.Source = FeatureTracker.ColourImageRecent.ToBitmapSource();
video.Source = ((Bitmap)Bitmap.FromFile("test1.png")).ToBitmapSource();
TrackingReady = false;
}
public Bitmap FeatureDetect(KinectSensor nui)
{
byte[] ColourClone = new byte[ColourImageRecent.Length];
Array.Copy(ColourImageRecent, ColourClone, ColourImageRecent.Length);
Bitmap test = (Bitmap)Bitmap.FromFile("test1.png");
test.RotateFlip(RotateFlipType.RotateNoneFlipY);
Image<Gray, Byte> ImageRecent = new Image<Gray, byte>(test);
SURFDetector surfCPU = new SURFDetector(2000, false);
VectorOfKeyPoint RecentKeyPoints;
Matrix<int> indices;
Matrix<float> dist;
Matrix<byte> mask;
bool MatchFailed = false;
// extract SURF features from the object image
RecentKeyPoints = surfCPU.DetectKeyPointsRaw(ImageRecent, null);
//Matrix<float> RecentDescriptors = surfCPU.ComputeDescriptorsRaw(ImageRecent, null, RecentKeyPoints);
//MKeyPoint[] RecentPoints = RecentKeyPoints.ToArray();
// don't feature detect on first attempt, just store image details for next attempt
#region
/*
if (KeyPointsOld == null)
{
KeyPointsOld = RecentKeyPoints;
PointsOld = RecentPoints;
DescriptorsOld = RecentDescriptors;
return ImageRecent.ToBitmap();
}
*/
#endregion
// Attempt to match points to their nearest neighbour
#region
/*
BruteForceMatcher SURFmatcher = new BruteForceMatcher(BruteForceMatcher.DistanceType.L2F32);
SURFmatcher.Add(RecentDescriptors);
int k = 5;
indices = new Matrix<int>(DescriptorsOld.Rows, k);
dist = new Matrix<float>(DescriptorsOld.Rows, k);
*/
// Match features, provide the top k matches
//SURFmatcher.KnnMatch(DescriptorsOld, indices, dist, k, null);
// Create mask and set to allow all features
//mask = new Matrix<byte>(dist.Rows, 1);
//mask.SetValue(255);
#endregion
//Features2DTracker.VoteForUniqueness(dist, 0.8, mask);
// Check number of good maches and for error and end matching if true
#region
//int nonZeroCount = CvInvoke.cvCountNonZero(mask);
//if (nonZeroCount < 5) MatchFailed = true;
/*
try
{
nonZeroCount = Features2DTracker.VoteForSizeAndOrientation(RecentKeyPoints, KeyPointsOld, indices, mask, 1.5, 20);
}
catch (SystemException)
{
MatchFailed = true;
}
if (nonZeroCount < 5) MatchFailed = true;
if (MatchFailed)
{
return ImageRecent.ToBitmap();
}
*/
#endregion
//DepthMapColourCoordsRecent = CreateDepthMap(nui, DepthImageRecent);
//PointDist[] FeatureDistances = DistanceToFeature(indices, mask, RecentPoints);
//Image<Rgb,Byte> rgbimage = ImageRecent.Convert<Rgb, Byte>();
//rgbimage = DrawPoints(FeatureDistances, rgbimage);
// Store recent image data for next feature detect.
//KeyPointsOld = RecentKeyPoints;
//PointsOld = RecentPoints;
//DescriptorsOld = RecentDescriptors;
//CreateDepthMap(nui, iva);
//rgbimage = CreateDepthImage(DepthMapColourCoordsRecent, rgbimage);
// Convert image back to a bitmap
count++;
//Bitmap bitmap3 = rgbimage.ToBitmap();
//bitmapstore = bitmap3;
//bitmap3.Save("test" + count.ToString() + ".png");
return null;
}
This is a little late, but I had a similar problem and thought I'd share my solution.
In my case I was processing the depth stream. The default resolution was 640x480, and Emgu just wasn't able to process the image fast enough to keep up with the frameready handler. As soon as I reduced the depth stream resolution to 320x240 the problem went away.
I also went a bit further and moved my image processing to a different thread which sped it up even more (do a search for ComponentDispatcher.ThreadIdle). I'm still not able to do 640x480 at a reasonable frame rate, but at least the image renders so I can see what's going on.
I've been playing around with NGif Animator for resizing animated gifs and it does resize but parts in many animated gifs I've tried get erased. I looked through the comments on that page and didn't see anyone else mention it.
To eliminate resizing as the cause I simply loop through the frames and save each one. Each frame is a System.Drawing.Image. Transparency is set to none (Color.Empty).
This is my test method currently:
GifDecoder gifDecoder = new GifDecoder();
MemoryStream memoryStream = new MemoryStream();
new BinaryWriter((Stream)memoryStream).Write(imageToResize); // byte array
memoryStream.Position = 0L;
gifDecoder.Read((Stream)memoryStream);
memoryStream.Dispose();
string filename = Guid.NewGuid().ToString().Replace("-", String.Empty) + ".gif";
string output = path + #"\" + filename;
AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
animatedGifEncoder.Start(output);
animatedGifEncoder.SetRepeat(gifDecoder.GetLoopCount());
animatedGifEncoder.SetQuality(10); // They say 20 is max quality will get, I've tried higher. Makes it a little bit better but black areas instead of gray. 10 is their default.
animatedGifEncoder.SetTransparent(Color.Empty); // This is default either way
int frameCount = gifDecoder.GetFrameCount();
int num = 0;
Image frame;
Image image = null;
for (int index = frameCount; num < index; ++num)
{
frame = gifDecoder.GetFrame(num);
animatedGifEncoder.SetDelay(gifDecoder.GetDelay(num));
string fname = #"C:\Development\images\frame_" + num.ToString() + ".gif";
if (File.Exists(fname)) { File.Delete(fname); }
frame.Save(fname);
animatedGifEncoder.AddFrame(image);
}
animatedGifEncoder.Finish();
Here's an example of what's happening:
The background is gone and it's gray.
It's supposed to look like:
Anyone have experience with NGif and know what would cause this? The first frame is always fine. It's the others after that have a problem so I'm guessing something isn't being reset from frame to frame (or re-read). I've been adding more things to their reset frame method but so far it hasn't helped. That now looks like:
protected void ResetFrame()
{
lastDispose = dispose;
lastRect = new Rectangle(ix, iy, iw, ih);
lastImage = image;
lastBgColor = bgColor;
delay = 0;
transparency = false; // I don't want transparency
lct = null;
act = null;
transIndex = -1;
}
There is actually a bug in their code, a byte array not being reset. Check the comments on their page for a solution
I'm trying to thumb an image as fast as possible regardless of the usage of resources to be used in my ImageList and listview and this is currently how i'm doing it but it seems to be slow:
public Image toThumbs(string file, int width, int height)
{
image = null;
aspectRatio = 1;
fullSizeImg = null;
try
{
fullSizeImg = Image.FromFile(file);
float w = fullSizeImg.Width;
float h = fullSizeImg.Height;
aspectRatio = w / h;
int xp = width;
int yp = height;
if (fullSizeImg.Width > width && fullSizeImg.Height > height)
{
if ((float)xp / yp > aspectRatio)
{
xp = (int)(yp * aspectRatio);
}
else
{
yp = (int)(xp / aspectRatio);
}
}
else if (fullSizeImg.Width != 0 && fullSizeImg.Height != 0)
{
xp = fullSizeImg.Width;
yp = fullSizeImg.Height;
}
image = new Bitmap(width, height);
graphics = Graphics.FromImage(image);
graphics.FillRectangle(Brushes.White, ((width - xp) / 2), (height - yp), xp, yp);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(fullSizeImg, new Rectangle(((width - xp) / 2), (height - yp), xp, yp));
graphics.Dispose();
fullSizeImg.Dispose();
}
catch (Exception)
{
image = null;
}
return image;
}
I'm not sure if the computation is the one that is slowing down the thumbnailing or maybe the classes itself that are being used are slow, if that is the case then what other alternatives can be use maybe a different computation or i need to import other classes or is there a third party libraries that can be used or i need to do a dll import or something? Please help me.
Edit: Just found a solution here
http://www.vbforums.com/showthread.php?t=342386
it extracts a thumbnail from a file
without reading the whole file. I was able to reduce the time about 40% when i used this.
Your calculations happen in fractions of a second. The call to DrawImage is most likely the slowest part of this (as that one is doing the scaling).
If you're needing this thumbnail image exactly once then I don't see much room for improvement here. If you're calling that method on the same image more than once, you should cache the thumbnails.
I use this mechanism which seems to be very fast.
BitmapFrame bi = BitmapFrame.Create(new Uri(value.ToString()), BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
// If this is a photo there should be a thumbnail image, this is VERY fast
if (bi.Thumbnail != null)
{
return bi.Thumbnail;
}
else
{
// No thumbnail so make our own (Not so fast)
BitmapImage bi2 = new BitmapImage();
bi2.BeginInit();
bi2.DecodePixelWidth = 100;
bi2.CacheOption = BitmapCacheOption.OnLoad;
bi2.UriSource = new Uri(value.ToString());
bi2.EndInit();
return bi2;
}
Hope this helps.
Out of curiosity, have you tried the GetThumbnailImage method on System.Drawing.Bitmap? It might at least be worth comparing to your current implementation.
This may seem like to much of an obvious answer but have you tried just using Image.GetThumbnailImage()?
You don't get as much control over the quality of the result but if speed is your main concern?
Your thumbnail extraction that gets you the big speed up relies on the image having a thumbnail already embedded in it.
To speed up the original you might find that changing:-
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
to
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
Might help.