I have been exhausted from this error from last 2 weeks.I tried a lot to find out and tried the code in different way but not succeeded yet.I think the main problem is with bitmap, may be i am not using in right way.I am sharing my code for help to understand what i am doing.
First i tell you the scenario.In this app, i am using dslr camera for live view.The main code area from camera class is here below :
internal void Run()
{
LVrunning = true;
while (LVrunning)
{
Thread.Sleep(20);
if (LVrunning)
UpdatePicture();
}
}
private void UpdatePicture()
{
try
{
if (err == EDSDK.EDS_ERR_OK && LVrunning)
{
inSide = true;
// Download live view image data
err = EDSDK.EdsDownloadEvfImage(cameraDev, EvfImageRef);
if (err != EDSDK.EDS_ERR_OK)
{
Debug.WriteLine(String.Format("Download of Evf Image: {0:X}", err));
return;
}
IntPtr ipData;
err = EDSDK.EdsGetPointer(MemStreamRef, out ipData);
if (err != EDSDK.EDS_ERR_OK)
{
Debug.WriteLine(String.Format("EdsGetPointer failed: {0:X}", err));
return;
}
uint len;
err = EDSDK.EdsGetLength(MemStreamRef, out len);
if (err != EDSDK.EDS_ERR_OK)
{
Debug.WriteLine(String.Format("EdsGetLength failed:{0:X}", err));
EDSDK.EdsRelease(ipData);
return;
}
Byte[] data = new byte[len];
Marshal.Copy(ipData, data, 0, (int)len);
System.IO.MemoryStream memStream = new System.IO.MemoryStream(data);
// get the bitmap
Bitmap bitmap = null;
try
{
bitmap = new Bitmap(memStream);
}
catch (OutOfMemoryException ex)
{
GC.WaitForPendingFinalizers();
bitmap = new Bitmap(memStream); // sometimes error occur
}
NewFrame(bitmap, null); // this is event call back to form area.
memStream.Dispose();
EDSDK.EdsRelease(ipData);
}
}
catch (Exception ex)
{
}
}
private void getCapturedItem(IntPtr directoryItem)
{
uint err = EDSDK.EDS_ERR_OK;
IntPtr stream = IntPtr.Zero;
EDSDK.EdsDirectoryItemInfo dirItemInfo;
err = EDSDK.EdsGetDirectoryItemInfo(directoryItem, out dirItemInfo);
if (err != EDSDK.EDS_ERR_OK)
{
throw new CameraException("Unable to get captured item info!", err);
}
// Fill the stream with the resulting image
if (err == EDSDK.EDS_ERR_OK)
{
err = EDSDK.EdsCreateMemoryStream((uint)dirItemInfo.Size, out stream);
}
// Copy the stream to a byte[] and
if (err == EDSDK.EDS_ERR_OK)
{
err = EDSDK.EdsDownload(directoryItem, (uint)dirItemInfo.Size, stream);
}
// Create the returned item
//CapturedItem item = new CapturedItem();
if (dirItemInfo.szFileName.ToString().ToLower().Contains("jpg") || dirItemInfo.szFileName.ToString().ToLower().Contains("jpeg"))
{
if (err == EDSDK.EDS_ERR_OK)
{
IntPtr imageRef = IntPtr.Zero;
err = EDSDK.EdsCreateImageRef(stream, out imageRef);
if (err == EDSDK.EDS_ERR_OK)
{
EDSDK.EdsImageInfo info;
err = EDSDK.EdsGetImageInfo(imageRef, EDSDK.EdsImageSource.FullView, out info);
}
}
}
if (err == EDSDK.EDS_ERR_OK)
{
try
{
byte[] buffer = new byte[(int)dirItemInfo.Size];
GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr address = gcHandle.AddrOfPinnedObject();
IntPtr streamPtr = IntPtr.Zero;
err = EDSDK.EdsGetPointer(stream, out streamPtr);
if (err != EDSDK.EDS_ERR_OK)
{
throw new CameraDownloadException("Unable to get resultant image.", err);
}
try
{
Marshal.Copy(streamPtr, buffer, 0, (int)dirItemInfo.Size);//sometimes error comes here
System.IO.MemoryStream memStream = new System.IO.MemoryStream(buffer);
Bitmap bitmap = null;
try
{
bitmap = new Bitmap(memStream);
}
catch (OutOfMemoryException ex)
{
GC.WaitForPendingFinalizers();
Bitmap b = new Bitmap(memStream);//sometimes error comes here
}
if (bitmap != null)
{
PhotoCaptured(bitmap, null);
}
}
catch (AccessViolationException ave)
{
throw new CameraDownloadException("Error copying unmanaged stream to managed byte[].", ave);
}
finally
{
gcHandle.Free();
EDSDK.EdsRelease(stream);
EDSDK.EdsRelease(streamPtr);
}
}
catch (OutOfMemoryException ex)
{
GC.WaitForPendingFinalizers();
IboothmeObject.minimizeMemory();
getCapturedItem(directoryItem);
}
}
else
{
throw new CameraDownloadException("Unable to get resultant image.", err);
}
}
On form side, image is updating in picture box simply
private void StartLiveView()
{
if (this.liveView.Connected)
{
this.liveView.PhotoCaptured += new EventHandler(liveView_PhotoCaptured);
this.liveView.NewFrame += new EventHandler(liveView_NewFrame);
this.liveView.StartLiveView();
}
}
void liveView_NewFrame(object sender, EventArgs e)
{
this.picMain.Image = sender as Image;
}
void liveView_PhotoCaptured(object sender, EventArgs e)
{
Image img = sender as Image;
// this image is big in size like 5000x3000.
Bitmap tempbitmap = new Bitmap(img.Width, img.Height);// now mostly error comes here
tempbitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using (Graphics g = Graphics.FromImage(tempbitmap))
{
g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
g.Save();
}
picMain.Image = tempbitmap;
tempbitmap.Save(path,ImageFormat.Jpeg);
}
Another area of code which uses the bitmap and live view from camera.This code get the frame from camera and write some objects on frame..In my case, i am writing some ballons on the frame
void liveView_NewFrame(object sender, EventArgs e)
{
using (Image<Bgr, byte> Frame = new Image<Bgr, byte>(new Bitmap(sender as Image)))
{
Frame._SmoothGaussian(3);
IntPtr hsvImage = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(Frame), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3);
CvInvoke.cvCvtColor(Frame, hsvImage, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2HSV);
Image<Gray, byte> imgThresh = new Image<Gray, byte>(Frame.Size);
imgThresh.Ptr = GetThresholdedImage(hsvImage);
//CvInvoke.cvSmooth(imgThresh, imgThresh, Emgu.CV.CvEnum.SMOOTH_TYPE.CV_GAUSSIAN, 3, 3, 3, 3);
#region Draw the contours of difference
//this is tasken from the ShapeDetection Example
Rectangle largest = new Rectangle();
try
{
using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
//detect the contours and loop through each of them
for (Contour<Point> contours = imgThresh.Convert<Gray, Byte>().FindContours(
Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL,
storage);
contours != null;
contours = contours.HNext)
{
//Create a contour for the current variable for us to work with
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
//Draw the detected contour on the image
if (currentContour.Area > ContourThresh) //only consider contours with area greater than 100 as default then take from form control
{
if (currentContour.BoundingRectangle.Width > largest.Width && currentContour.BoundingRectangle.Height > largest.Height)
{
largest = currentContour.BoundingRectangle;
}
}
//storage.Dispose();
}
}
catch (Exception)
{
}
#endregion
#region Draw Object
Random r = new Random();
//Bitmap bb = Frame.Bitmap;
foreach (var item in objectList)
{
using (Graphics g = Graphics.FromImage(Frame.Bitmap))
{
if (DrawAble(item, largest))
{
if (item.Y < 0)
{
if (item.X < picMain.Width)
{
g.DrawImage(item.image, new Rectangle(item.X, 0, item.image.Width, item.image.Height + item.Y),
new Rectangle(), GraphicsUnit.Pixel);
item.X += r.Next(-5, 5);
item.Y += 15;
}
}
else
{
if (item.X < picMain.Width && item.Y < picMain.Height)
{
g.DrawImage(item.image, new Rectangle(item.X, item.Y, item.image.Width, item.image.Height));
item.X += r.Next(-5, 5);
item.Y += 15;
}
else
{
item.X = r.Next(0, picMain.Width - 5);
item.Y = r.Next(-item.image.Height, -5);
}
}
}
else
{
item.X = r.Next(0, picMain.Width - 5);
item.Y = r.Next(-item.image.Height, -5);
}
}
}
#endregion
picMain.Image = Frame.ToBitmap();
}
minimizeMemory();
}
Now i share the whole problem in detail.
First on all i created a form for live view and by using opencv(Emgu) library , i am drawing balloons on the frame.In live view these balloons are moving.The other form is for capture the picture from camera with high resolution.
I noticed that, my application memory was increasing with every frame and after 2 live-view and 2 pictures caputured, it goes to 1+ GB.If i tried to show first form again for live view, error occured in UpdatePicture() function.
then i add the code to minimize the memory of the current application.now i am calling this function after every frame in live-view.
After this solution when i checked the memory of application it does not go over 100mb or 200mb.
But problem was still there.after few captures, error occurred in UpdatePicture() when get bitmap from stream ( bitmap = new Bitmap(memStream);).The error was same out of memory.
After some search i found this solution.
// get the bitmap
Bitmap bitmap = null;
try
{
bitmap = new Bitmap(memStream);
}
catch (OutOfMemoryException ex)
{
GC.WaitForPendingFinalizers();
bitmap = new Bitmap(memStream);
}
But not working error is still same.
Error sometimes shown in UpdatePicture() method, sometime occurred in liveView_NewFrame method.
Means problem is related to bitmap, bitmap size or memory is corrupt.
So please help me.I am worried, 2 weeks passed but i could not solve this.
you are calling CvInvoke.cvCreateImage the created data is sitting on the native heap
so it wont be collected by the GC.
you must call cvReleaseImage(IntPtr) to release the data
there are alot of memory profilers which can help understanding the issue
try using ANTS Memory Profiler 7
Related
I am trying to create code to export out the images within a PDF using iText Version 7.19. I'm having some issues with Flate encoded images. All the Flate encoded images from the Microsoft free book I'm using as an example (see Moving to Microsoft Visual Studio 2010) always coming out pink and depending upon how I try to copy the bytes they can come out distorted.
If I attempt to copy all the image bytes at once (see the SaveFlateEncodedImage2 method in the code below), they come out distorted like this one:
If I attempt to copy them row by row (see the SaveFlateEncodedImage method in the code below), they are pink like this one
Here is the code that I'm using to export them:
using iText.Kernel;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Filters;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace ITextPdfStuff
{
public class MyPdfImageExtractor
{
private readonly string _pdfFileName;
public MyPdfImageExtractor(string pdfFileName)
{
_pdfFileName = pdfFileName;
}
public void ExtractToDirectory(string directoryName)
{
using (var reader = new PdfReader(_pdfFileName))
{
// Avoid iText.Kernel.Crypto.BadPasswordException: https://stackoverflow.com/a/48065052/97803
reader.SetUnethicalReading(true);
using (var pdfDoc = new PdfDocument(reader))
{
ExtractImagesOnAllPages(pdfDoc, directoryName);
}
}
}
private void ExtractImagesOnAllPages(PdfDocument pdfDoc, string directoryName)
{
Console.WriteLine($"Number of pdf {pdfDoc.GetNumberOfPdfObjects()} objects");
// Extract objects https://itextpdf.com/en/resources/examples/itext-7/extracting-objects-pdf
for (int objNumber = 1; objNumber <= pdfDoc.GetNumberOfPdfObjects(); objNumber++)
{
PdfObject currentObject = pdfDoc.GetPdfObject(objNumber);
if (currentObject != null && currentObject.IsStream())
{
try
{
ExtractImagesOneImage(currentObject as PdfStream, Path.Combine(directoryName, $"image{objNumber}.png"));
}
catch (Exception ex)
{
Console.WriteLine($"Object number {objNumber} is NOT an image! -- error: {ex.Message}");
}
}
}
}
private void ExtractImagesOneImage(PdfStream someStream, string fileName)
{
var pdfDict = (PdfDictionary)someStream;
string subType = pdfDict.Get(PdfName.Subtype)?.ToString() ?? string.Empty;
bool isImage = subType == "/Image";
if (isImage == false)
return;
bool decoded = false;
string filter = pdfDict.Get(PdfName.Filter).ToString();
if (filter == "/FlateDecode")
{
SaveFlateEncodedImage(fileName, pdfDict, someStream.GetBytes(false));
}
else
{
byte[] imgData;
try
{
imgData = someStream.GetBytes(decoded);
}
catch (PdfException ex)
{
imgData = someStream.GetBytes(!decoded);
}
SaveNormalImage(fileName, imgData);
}
}
private void SaveNormalImage(string fileName, byte[] imgData)
{
using (var memStream = new System.IO.MemoryStream(imgData))
using (var image = System.Drawing.Image.FromStream(memStream))
{
image.Save(fileName, ImageFormat.Png);
Console.WriteLine($"{Path.GetFileName(fileName)}");
}
}
private void SaveFlateEncodedImage(string fileName, PdfDictionary pdfDict, byte[] imgData)
{
int width = int.Parse(pdfDict.Get(PdfName.Width).ToString());
int height = int.Parse(pdfDict.Get(PdfName.Height).ToString());
int bpp = int.Parse(pdfDict.Get(PdfName.BitsPerComponent).ToString());
// Example that helped: https://stackoverflow.com/a/8517377/97803
PixelFormat pixelFormat;
switch (bpp)
{
case 1:
pixelFormat = PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bpp);
}
// .NET docs https://api.itextpdf.com/iText7/dotnet/7.1.9/classi_text_1_1_kernel_1_1_pdf_1_1_filters_1_1_flate_decode_strict_filter.html
// Java docs have more detail: https://api.itextpdf.com/iText7/java/7.1.7/com/itextpdf/kernel/pdf/filters/FlateDecodeFilter.html
imgData = FlateDecodeStrictFilter.FlateDecode(imgData, true);
// byte[] streamBytes = FlateDecodeStrictFilter.DecodePredictor(imgData, pdfDict);
// Copy the image one row at a time
using (var bmp = new Bitmap(width, height, pixelFormat))
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat);
int length = (int)Math.Ceiling(width * bpp / 8.0);
for (int i = 0; i < height; i++)
{
int offset = i * length;
int scanOffset = i * bmpData.Stride;
Marshal.Copy(imgData, offset, new IntPtr(bmpData.Scan0.ToInt64() + scanOffset), length);
}
bmp.UnlockBits(bmpData);
bmp.Save(fileName, ImageFormat.Png);
}
Console.WriteLine($"FlateDecode! {Path.GetFileName(fileName)}");
}
/// <summary>This method distorts the image badly</summary>
private void SaveFlateEncodedImage2(string fileName, PdfDictionary pdfDict, byte[] imgData)
{
int width = int.Parse(pdfDict.Get(PdfName.Width).ToString());
int height = int.Parse(pdfDict.Get(PdfName.Height).ToString());
int bpp = int.Parse(pdfDict.Get(PdfName.BitsPerComponent).ToString());
// Example that helped: https://stackoverflow.com/a/8517377/97803
PixelFormat pixelFormat;
switch (bpp)
{
case 1:
pixelFormat = PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bpp);
}
// .NET docs https://api.itextpdf.com/iText7/dotnet/7.1.9/classi_text_1_1_kernel_1_1_pdf_1_1_filters_1_1_flate_decode_strict_filter.html
// Java docs have more detail: https://api.itextpdf.com/iText7/java/7.1.7/com/itextpdf/kernel/pdf/filters/FlateDecodeFilter.html
imgData = FlateDecodeStrictFilter.FlateDecode(imgData, true);
// byte[] streamBytes = FlateDecodeStrictFilter.DecodePredictor(imgData, pdfDict);
// Copy the entire image in one go
using (var bmp = new Bitmap(width, height, pixelFormat))
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat);
Marshal.Copy(imgData, 0, bmpData.Scan0, imgData.Length);
bmp.UnlockBits(bmpData);
bmp.Save(fileName, ImageFormat.Png);
}
Console.WriteLine($"FlateDecode! {Path.GetFileName(fileName)}");
}
}
}
The code can be instantiated and called like this from within a .NET Core console application:
string existingFileName = #"c:\temp\ReallyLongBook1.pdf";
var imageExtractor = new MyPdfImageExtractor(existingFileName);
imageExtractor.ExtractToDirectory(#"c:\temp\images");
I'm running the following free Microsoft book through this code:
Moving to Microsoft Visual Studio 2010
The image in question is on page 10 and it's black and white (not pink).
I'm no PDF expert and I've been banging on this code for a couple of days now picking apart a number of examples to try to piece this together. Any help that would get me past my pink images, would be greatly appreciated.
-------Update Feb 4, 2020------
Here is the revised version after MKL's suggested changes. His change extracted more images than mine and produced proper looking images that appear in the book I mentioned above:
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Data;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf.Xobject;
using System;
using System.Collections.Generic;
using System.IO;
namespace ITextPdfStuff
{
public class MyPdfImageExtractor
{
private readonly string _pdfFileName;
public MyPdfImageExtractor(string pdfFileName)
{
_pdfFileName = pdfFileName;
}
public void ExtractToDirectory(string directoryName)
{
using (var reader = new PdfReader(_pdfFileName))
{
// Avoid iText.Kernel.Crypto.BadPasswordException: https://stackoverflow.com/a/48065052/97803
reader.SetUnethicalReading(true);
using (var pdfDoc = new PdfDocument(reader))
{
ExtractImagesOnAllPages(pdfDoc, directoryName);
}
}
}
private void ExtractImagesOnAllPages(PdfDocument pdfDoc, string directoryName)
{
Console.WriteLine($"Number of pdf {pdfDoc.GetNumberOfPdfObjects()} objects");
IEventListener strategy = new ImageRenderListener(Path.Combine(directoryName, #"image{0}.{1}"));
PdfCanvasProcessor parser = new PdfCanvasProcessor(strategy);
for (var i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
parser.ProcessPageContent(pdfDoc.GetPage(i));
}
}
}
public class ImageRenderListener : IEventListener
{
public ImageRenderListener(string format)
{
this.format = format;
}
public void EventOccurred(IEventData data, EventType type)
{
if (data is ImageRenderInfo imageData)
{
try
{
PdfImageXObject imageObject = imageData.GetImage();
if (imageObject == null)
{
Console.WriteLine("Image could not be read.");
}
else
{
File.WriteAllBytes(string.Format(format, index++, imageObject.IdentifyImageFileExtension()), imageObject.GetImageBytes());
}
}
catch (Exception ex)
{
Console.WriteLine("Image could not be read: {0}.", ex.Message);
}
}
}
public ICollection<EventType> GetSupportedEvents()
{
return null;
}
string format;
int index = 0;
}
}
PDFs internally support a very flexible bitmap image format, in particular as far as different color spaces are concerned.
iText in its parsing API supports export of a subset thereof, essentially the subset of images that easily can be exported as regular JPEGs or PNGs.
Thus, it makes sense to try and export using the iText parsing API first. You can do that as follows:
Directory.CreateDirectory(#"extract\");
using (PdfReader reader = new PdfReader(#"Moving to Microsoft Visual Studio 2010 ebook.pdf"))
using (PdfDocument pdfDocument = new PdfDocument(reader))
{
IEventListener strategy = new ImageRenderListener(#"extract\Moving to Microsoft Visual Studio 2010 ebook-i7-{0}.{1}");
PdfCanvasProcessor parser = new PdfCanvasProcessor(strategy);
for (var i = 1; i <= pdfDocument.GetNumberOfPages(); i++)
{
parser.ProcessPageContent(pdfDocument.GetPage(i));
}
}
with the helper class ImageRenderListener:
public class ImageRenderListener : IEventListener
{
public ImageRenderListener(string format)
{
this.format = format;
}
public void EventOccurred(IEventData data, EventType type)
{
if (data is ImageRenderInfo imageData)
{
try
{
PdfImageXObject imageObject = imageData.GetImage();
if (imageObject == null)
{
Console.WriteLine("Image could not be read.");
}
else
{
File.WriteAllBytes(string.Format(format, index++, imageObject.IdentifyImageFileExtension()), imageObject.GetImageBytes());
}
}
catch (Exception ex)
{
Console.WriteLine("Image could not be read: {0}.", ex.Message);
}
}
}
public ICollection<EventType> GetSupportedEvents()
{
return null;
}
string format;
int index = 0;
}
In case of your example document it exports nearly 400 images successfully, among them your example image above:
But there also are less than 30 images it cannot export, on standard out you'll find "Image could not be read: The color space /DeviceN is not supported.."
I'm working on a service for a company project that handles image processing, and one of the methods is supposed to clean the metadata from an image passed to it.
I think implementation I currently have works, but I'm not sure if it's affecting the quality of images or if there's a better way to handle this task. Could you let me know if you know of a better way to do this?
Here's the method in question:
public byte[] CleanMetadata(byte[] data)
{
Image image;
if (tryGetImageFromBytes(data, out image))
{
Bitmap bitmap = new Bitmap(image);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(image, new Point(0, 0));
}
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(image, typeof(byte[]));
}
return null;
}
And, for reference, the tryGetImageFromBytes method:
private bool tryGetImageFromBytes(byte[] data, out Image image)
{
try
{
using (var ms = new MemoryStream(data))
{
image = Image.FromStream(ms);
}
}
catch (ArgumentException)
{
image = null;
return false;
}
return true;
}
To reiterate: is there a better way to remove metadata from an image that doesn't involve redrawing it?
Thanks in advance.
The .NET way: You may want to try your hand at the System.Windows.Media.Imaging.BitmapEncoder class - more precisely, its Metadata collection. Quoting MSDN:
Metadata - Gets or sets the metadata that will be associated with this
bitmap during encoding.
The 'Oops, I (not so accidentally) forgot something way: Open the original bitmap file into a System.drawing.Bitmap object. Clone it to a new Bitmap object. Write the clone's contents to a new file. Like this one-liner:
((System.Drawing.Bitmap)System.Drawing.Image.FromFile(#"C:\file.png").Clone()).Save(#"C:\file-nometa.png");
The direct file manipulation way (only for JPEG): Blog post about removing the EXIF area.
I would suggest this, the source is here: Removing Exif-Data for jpg file
Changing a bit the 1st function
public Stream PatchAwayExif(Stream inStream)
{
Stream outStream = new MemoryStream();
byte[] jpegHeader = new byte[2];
jpegHeader[0] = (byte)inStream.ReadByte();
jpegHeader[1] = (byte)inStream.ReadByte();
if (jpegHeader[0] == 0xff && jpegHeader[1] == 0xd8) //check if it's a jpeg file
{
SkipAppHeaderSection(inStream);
}
outStream.WriteByte(0xff);
outStream.WriteByte(0xd8);
int readCount;
byte[] readBuffer = new byte[4096];
while ((readCount = inStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
outStream.Write(readBuffer, 0, readCount);
return outStream;
}
And the second function with no changes, as post
private void SkipAppHeaderSection(Stream inStream)
{
byte[] header = new byte[2];
header[0] = (byte)inStream.ReadByte();
header[1] = (byte)inStream.ReadByte();
while (header[0] == 0xff && (header[1] >= 0xe0 && header[1] <= 0xef))
{
int exifLength = inStream.ReadByte();
exifLength = exifLength << 8;
exifLength |= inStream.ReadByte();
for (int i = 0; i < exifLength - 2; i++)
{
inStream.ReadByte();
}
header[0] = (byte)inStream.ReadByte();
header[1] = (byte)inStream.ReadByte();
}
inStream.Position -= 2; //skip back two bytes
}
Creating a new bitmap will clear out all the exif data.
var newImage = new Bitmap(image);
If you want to remove only specific info:
private Image RemoveGpsExifInfo(Image image)
{
foreach (var item in image.PropertyItems)
{
// GPS range is from 0x0000 to 0x001F. Full list here -> https://exiftool.org/TagNames/EXIF.html (click on GPS tags)
if (item.Id <= 0x001F)
{
image.RemovePropertyItem(item.Id);
}
}
return image;
}
I have one app sending screenshots to another app that needs to present them in the WPF window
it works great except for the only probelm which is that the code builds up in the memory each time i add a new background.
how do I solve this problem ?
Thanks!
private void GetSnapshots(object state)
{
using (var socket=new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect(new IPEndPoint(IPAddress.Parse("10.0.0.9"), 8081));
while (Connected)
{
var lengthData = new byte[4];
var lengthBytesRead = 0;
while (lengthBytesRead < lengthData.Length)
{
var read = socket.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
if (read == 0) return;
lengthBytesRead += read;
}
var length = BitConverter.ToInt32(lengthData, 0);
var imageData = new byte[length];
var imageBytesRead = 0;
while (imageBytesRead < imageData.Length)
{
var read = socket.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead, SocketFlags.None);
if (read == 0) return;
imageBytesRead += read;
}
using (var stream = new MemoryStream(imageData))
{
var bitmap = new Bitmap(stream);
Dispatcher.Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
stream.Dispose();
bitmap.Dispose();
}
}
socket.Disconnect(false);
}
}
public static System.Windows.Media.Brush CreateBrushFromBitmap(Bitmap bmp)
{
return new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
}
private delegate void ImageCompleteDelegate(Bitmap bitmap);
private void ImageComplete(Bitmap bitmap)
{
if (_buffer != null)
{
_buffer = null;
}
_buffer = new Bitmap(bitmap);
bitmap.Dispose();
//ScreenShotG is a Grid Element inside the XAML
ScreenShotG.Background = CreateBrushFromBitmap(_buffer);
}
I finally fixed it after many hours of researching and testing.
The problem was in the following code :
public static System.Windows.Media.Brush CreateBrushFromBitmap(Bitmap bmp)
{
return new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
}
The solution was :
public static System.Windows.Media.Brush CreateBrushFromBitmap(Bitmap bmp)
{
IntPtr hBitMap = bmp.GetHbitmap();
ImageBrush b = new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
DeleteObject(hBitMap);
return b;
}
I was trying a basic "hello world" app for a first kinect project, to stream the video data to the stream. The code is here: https://github.com/fschwiet/HelloKinect/blob/8803b6b959ee6dba5f9284b9e732fb11a897dea4/HelloKinect/ShowCameraCommand.cs
What I find is that I can poll for frame data in a loop, but I am not receiving the frame-ready events. The sourcecode is below. When UsePolling is true, frame data is sent to the form. When UsePolling is false, there is console output "Hit return to exit." indicating everything has run, but no events are ever received.
I have a feeling this has to do with windows message pumps, that I need to wait in an alertable state and/or pump a message queue. I haven't been able to make it work though, anyone have any hints?
public class ShowCameraCommand : ConsoleCommand
{
static private Form EchoForm;
private bool UsePolling;
public ShowCameraCommand()
{
this.IsCommand("show-camera");
this.HasOption("p", "Use polling to check frame data", v => UsePolling = true);
}
public override int Run(string[] remainingArguments)
{
var sensor = KinectSensor.KinectSensors.Where(s => s.Status == KinectStatus.Connected).FirstOrDefault();
if (sensor == null)
{
Console.WriteLine("Kinect was not detected");
Console.WriteLine();
return -1;
}
EchoForm = new Form();
EchoForm.Width = 640;
EchoForm.Height = 480;
EchoForm.Show();
sensor.ColorStream.Enable(ColorImageFormat.RawYuvResolution640x480Fps15);
if (!UsePolling)
{
sensor.ColorFrameReady += sensor_ColorFrameReady;
}
sensor.Start();
if (UsePolling)
{
Console.WriteLine("Use any key to exit.");
while (!Console.KeyAvailable)
{
using (var frame = sensor.ColorStream.OpenNextFrame(10 * 1000))
{
HandleFrame(frame);
}
Thread.Sleep(50);
}
}
else
{
Console.WriteLine("Hit return to exit.");
Console.ReadLine();
}
return 0;
}
void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
Console.WriteLine("Frame received");
using (ColorImageFrame frame = e.OpenColorImageFrame())
{
HandleFrame(frame);
}
}
private void HandleFrame(ColorImageFrame frame)
{
var bitmap = ImageToBitmap(frame);
using (var g = EchoForm.CreateGraphics())
{
g.DrawImage(bitmap, 0, 0);
Console.WriteLine("Frame drawn");
}
}
// http://stackoverflow.com/questions/10848190/convert-kinect-colorframe-to-bitmap
Bitmap ImageToBitmap(ColorImageFrame Image)
{
byte[] pixeldata = new byte[Image.PixelDataLength];
Image.CopyPixelDataTo(pixeldata);
Bitmap bmap = new Bitmap(Image.Width, Image.Height, PixelFormat.Format32bppRgb);
BitmapData bmapdata = bmap.LockBits(
new Rectangle(0, 0, Image.Width, Image.Height),
ImageLockMode.WriteOnly,
bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;
Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength);
bmap.UnlockBits(bmapdata);
return bmap;
}
}
Oh, figured it out. I needed to call Application.Run() to start pumping events.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Troubleshooting .NET “Fatal Execution Engine Error”
My code is throwing a Fatal Execution Error. The exact error is this:
The runtime has encountered a fatal error. The address of the error
was at 0xed40646c, on thread 0x2044. The error code is 0xc0000005.
This error may be a bug in the CLR or in the unsafe or non-verifiable
portions of user code. Common sources of this bug include user
marshaling errors for COM-interop or PInvoke, which may corrupt the
stack.
I'm not using unsafe user code as far as I know.
The code which is causing the issue is this one:
WebClient client = new WebClient();
string pageHtml = client.DownloadString(url);
browser.ScriptErrorsSuppressed = true;
browser.DocumentText = pageHtml;
do
{
Application.DoEvents();
} while (browser.ReadyState != WebBrowserReadyState.Complete); //CRASH OCCURS HERE
Now here's the kicker. This code is beign run on a loop. Every so often, it gives this error. Sometimes its on the 1000th run. Last time it was on the 5545th run. It seems to be very random indeed.
How do I solve this problem? Or how can I get more information to solve it?
My solution is based on How to wait until WebBrowser is completely loaded in VB.NET?
What u have to do is to add bool _pageReady variable to Completed event.
void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var urlCurrent = e.Url.ToString();
var browser = (WebBrowser)sender;
if (!(urlCurrent.StartsWith("http://") || urlCurrent.StartsWith("https://")))
{
// in AJAX
}
if (e.Url.AbsolutePath != browser.Url.AbsolutePath)
{
// IFRAME
}
else
{
// DOCUMENT IS LOADED 100%
Debug.WriteLine("DocumentCompleted " + DateTime.Now.TimeOfDay.ToString());
_pageReady = true; // Here it goes!!!! :)
try
{
mshtml.IHTMLDocument2 docs2 = (mshtml.IHTMLDocument2)web.Document.DomDocument;
mshtml.IHTMLDocument3 docs3 = (mshtml.IHTMLDocument3)web.Document.DomDocument;
mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2)docs2.body;
mshtml.IHTMLElement2 root2 = (mshtml.IHTMLElement2)docs3.documentElement;
// Determine dimensions for the image; we could add minWidth here
// to ensure that we get closer to the minimal width (the width
// computed might be a few pixels less than what we want).
int width = Math.Max(body2.scrollWidth, root2.scrollWidth);
int height = Math.Max(root2.scrollHeight, body2.scrollHeight);
//get the size of the document's body
Rectangle docRectangle = new Rectangle(0, 0, width, height);
web.Width = docRectangle.Width;
web.Height = docRectangle.Height;
//if the imgsize is null, the size of the image will
//be the same as the size of webbrowser object
//otherwise set the image size to imgsize
Rectangle imgRectangle;
if (imgsize == null) imgRectangle = docRectangle;
else imgRectangle = new System.Drawing.Rectangle() { Location = new System.Drawing.Point(0, 0), Size = imgsize.Value };
//create a bitmap object
__Bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height);
//Rectangle resolution = Screen.PrimaryScreen.Bounds;
//__Bitmap.SetResolution(resolution.Width, resolution.Height);
//get the viewobject of the WebBrowser
IViewObject ivo = web.Document.DomDocument as IViewObject;
using (Graphics g = Graphics.FromImage(__Bitmap))
{
//get the handle to the device context and draw
IntPtr hdc = g.GetHdc();
ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero,
IntPtr.Zero, hdc, ref imgRectangle,
ref docRectangle, IntPtr.Zero, 0);
g.ReleaseHdc(hdc);
}
//var randomPart = System.IO.Path.GetRandomFileName();
//__Bitmap.Save(#"D:\t" + randomPart + ".png");
if (CropRectangle != null)
{
if (CropRectangle.Width > 0 && CropRectangle.Height > 0)
{
Bitmap bmpCrop = __Bitmap.Clone(CropRectangle, __Bitmap.PixelFormat);
__Bitmap = bmpCrop;
}
}
//__Bitmap.Save(#"D:\cropped" + randomPart + ".png");
bitmapPointer = __Bitmap.GetHbitmap();
}
catch
{
//System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
}
and also to do something like
public void HtmlCapture2()
{
try
{
if (web == null)
web = InitWebBrowser();
web.Navigate(_navigateURL);
try
{
while (_pageReady == false) // YEAH!!!!!! IT IS WORKING!!!!
{
System.Windows.Forms.Application.DoEvents();
}
//Thread.Sleep(WaitForWebsite); --- It works but....
//while (web.ReadyState != WebBrowserReadyState.Complete) --- it gives an ERROR
// System.Windows.Forms.Application.DoEvents();
}
catch (Exception)
{
}
}
catch (Exception)
{
}
}