Windows 7 TextureBrush..ctor() error - windows-7

I have a .NET 2.0 app that runs just fine on XP and Vista, but on Windows 7 RC (x64) it crashes with the following error:
Exception Information
Exception Type: System.OutOfMemoryException
Message: Out of memory.
Data: System.Collections.ListDictionaryInternal
TargetSite: Void .ctor(System.Drawing.Image, System.Drawing.Drawing2D.WrapMode)
HelpLink: NULL
Source: System.Drawing
StackTrace Information
at System.Drawing.TextureBrush..ctor(Image image, WrapMode wrapMode)
at System.Windows.Forms.ControlPaint.DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft)
at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
Any ideas about why this is happening, or how I might program around it? It's just painting a standard winform with no special background.
UPDATE:
I've found that this is only an issue when the BackgroundImageLayout = ImageLayout.Tile, which is also the default. Set it to Zoom or Center, and the issue dissapears. That's pretty unsatisfactory though, because I need it to tile.

I had a similar problem. In my case I had disposed of my MemoryStream I loaded the image from.
//The following throws and OutOfMemoryException at the TextureBrush.ctor():
/*someBytes and g declared somewhere up here*/
Bitmap myBmp = null;
using(MemoryStream ms = new MemoryStream(someBytes))
myBmp = new Bitmap(ms);
if(myBmp != null) //that's right it's not null.
using(TextureBrush tb = new TextureBrush(myBmp)) //OutOfMemoryException thrown
g.FillRectangle(tb,0,0,50,50);
//This code does not throw the same error:
/*someBytes and g declared somewhere up here*/
MemoryStream ms = new MemoryStream(someBytes);
Bitmap myBmp = new Bitmap(ms);
if(myBmp != null)
using(TextureBrush tb = new TextureBrush(myBmp))
g.FillRectangle(tb,0,0,50,50);

Turns out the solution to this had to do with the PNG file itself used for the background.
I just opened it with Paint.NET and resaved it, then put it back in the project and it worked.
Not sure what changed, but it resolved the problem.

Please do not dispose the Image or close the filestream object from where you have got the Image before calling the TextureBrush class for tiling. Otherwise the TextureBrush class will throw an Out of Memory exception.
So the better way is to show the tiled Image by calling the TextureBrush Image and then close the filestream object in the Paint event of the windows form.

Related

GDI: Original DC bitmap changes, and can't restore

I'm seeing an issue where I could not re-select the original bitmap on a DC, causing a memory leak. The pointer to the original bitmap stayed the same throughout the program, but the data (from CBitmap::GetBitmap) changes from monochrome to something else. I don't know when the bitmap actually changes, but something in the system is causing it.
CBitmap* cMyClass::mpOldBitmap;
CDC cMyClass::mCanvasDc;
CBitmap cMyClass::mCanvasBmp;
void cMyClass::Init()
{
// One-time initialization
CDC* pDc = GetDC();
mCanvasDc.CreateCompatibleDC(pDc);
mCanvasBmp.CreateCompatibleBitmap(pDc, 10, 10);
mpOldBitmap = mCanvasDc.SelectObject(&mCanvasBmp);
ReleaseDC(pDc);
BITMAP bitmap;
mpOldBitmap->GetBitmap(&bitmap); // A monochrome bitmap, as expected.
}
void cMyClass::Recreate(int newW, int newH)
{
// 1. Delete existing bitmap:
if (mpOldBitmap)
{
BITMAP bitmap;
mpOldBitmap->GetBitmap(&bitmap); // This is no longer the monochrome bitmap. It is 8bpp, with random size.
CBitmap* pCurrBmp = mCanvasDc.SelectObject(mpOldBitmap); // This fails (NULL). I can't de-select my bitmap.
mCanvasBmp.DeleteObject(); // This fails too, causing memory leak. Actually, it fails in CE6, but not in Win32. Regardless, both platforms will have a memory leak.
}
// 2. Recreate the bitmap with new size:
{
CDC* pDc = GetDC();
mCanvasBmp.CreateCompatibleBitmap(pDc, newW, newH);
ReleaseDC(pDc);
}
// 3. Finalize
mpOldBitmap = mCanvasDc.SelectObject(&mCanvasBmp);
}
Any known scenarios where this can happen?
Any debugging tips to break when the bitmap data changes?
Note: In code, I mentioned "this fails". I remove the assert on the returned values to make the code readable.
Edit: The solution that I am using to fix it is to use CDC:SaveDC and CDC::RestoreDC instead of stashing the pointer. The memory leak went away, and every GDI call passed. But I am still curious why the original code leaked. The pointer to the default bitmap, as far as I know, should be a default monochrome bitmap that is probably global in GDI world
Let's see the code from OP.
mpOldBitmap = mCanvasDc.SelectObject(mCanvasBmp);
Because mCanvasBmp is a CBitmap object (not a pointer to CBitmap), first is called the HGDIOBJ operator, then CDC::SelectObject(HGDIOBJ) which returns HGDIOBJ and not CBitmap*. This should give a conversion compiler error. If cast the returned value to CBitmap* is also wrong.
The correct way to get rid of problem is to pass a pointer.
mpOldBitmap = mCanvasDc.SelectObject(& mCanvasBmp);
This case will be called CDC::SelectObject(CBitmap* pBitmap) which returns a CBitmap*.
// I hope it's pretty clear. :)

Check if exact image in exact location is on screen

I am looking to create a program in Visual Studio (C#) which scans the screen for an exact image in an exact location of the screen. I have seen many discussions which involve algorithms to find a "close" image, but mine will be 100% exact; location, size and all.
I have obtained a png from a section of my screen [Image 1] using this code:
private void button1_Click(object sender, EventArgs e)
{
//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(1555, 950,
1700, 1010,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen.
bmpScreenshot.Save("Screenshot.png");
}
So, basically here is the flowchart of my program on how I want to move forward:
1) create the master png using the above code
2) run loop:
create same screenshot using the same procedure as the master png
compare master png to new screenshot png and if:match then move on otherwise reiterate loop.
I am very new to programming, but I don't believe this is beyond me, given a little guidance. I have written fairly complicated (in my opinion) VBA and Matlab programs. Any help is greatly appreciated.
Thank You,
Sloan
Digging around a bit through Microsoft's documentation, I came up with a rough function that would do something similar to what you want.
https://msdn.microsoft.com/en-us/library/hh191601.aspx
This function offers the chance of getting stuck in an endless loop, so you might consider calling it with a timeout from your main. See here for info on synchronous methods with timeouts:
Monitoring a synchronous method for timeout
From your main, all you'd have to do is see if it returns true.
static int Main(string[] args)
{
if (ImageInLocation(left, right, top, bottom)) {
// do other things
}
return 0;
}
The only thing I'm not entirely sure on is how strict you can be with the ColorDifference. Even if the images are identical, any pixel difference with an entirely non-tolerant ColorDifference will come up false. If you know it should work and it's not, perhaps consider increasing the tolerance. Here's some more info on that:
https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting.colordifference.aspx
public bool ImageInLocation(int left, int right, int top, int bottom) {
bool image_found = false;
var masterImage = Image.FromFile("path_to_master");
while (!image_found) {
// screenshot code above, output to "path_to/Screenshot.jpg"
var compImage = Image.FromFile("path_to/Screenshot.jpg");
// note, all zeroes may not be tolerant enough
var color_diff = new ColorDifference(0, 0, 0, 0);
Image diffImage;
image_found = ImageComparer.Compare(masterImage, compImage, color_diff, out diffImage);
}
return true;
}
Good luck! Welcome to the programming community.
Also, if anyone has any suggestions/changes, feel free to edit this. Happy imaging, friends!

Application crashes when asking for a frame that involves COORDINATE_FRAME_CAMERA_DEPTH

I'm using the JPointCloud sample app, and modifying it a little bit:
In JPointCloud.java : SetUpExtrinsics(), I added:
TangoPoseData depth2devicePose = new TangoPoseData();
framePair.baseFrame = TangoPoseData.COORDINATE_FRAME_DEVICE;
framePair.targetFrame = TangoPoseData.COORDINATE_FRAME_CAMERA_DEPTH;
try {
depth2devicePose = mTango.getPoseAtTime(0.0, framePair);
} catch (TangoErrorException e) {
Toast.makeText(getApplicationContext(), R.string.TangoError,
Toast.LENGTH_SHORT).show();
The application crashes when reaching the line:
depth2devicePose = mTango.getPoseAtTime(0.0, framePair);
I tried with other combinations of frame, but each time COORDINATE_FRAME_CAMERA_DEPTH is included, the app crashes.
Did I forget something ? Maybe to ask some kind of special permission for the depth camera ?
It indeed crashes on me as well.
However, according to the Java pointcloud example code (extrinsic query part), the correct way of querying the DEPTH_CAMERA w.r.t(with respect to) DEVICE transformation is using 'the inverse of DEVICE w.r.t IMU' multiply 'CAMAER w.r.t IMU', which is:
deive_T_camera = inverse(imu_T_device) * imu_T_camera
Also, note that the color camera, depth camera and camera are the same camera on the hardware level, so they actually share the same extrinsic.
Hope this helps.
Edit: the crashes actually throw an com.google.atap.tangoservice.TangoInvalidException. This might just be a frame pair not supported from API level, as mentioned before, the suggested way would be using other two matrices to compose the desired matrix in this case..

Android5.0 Bitmap.copyPixelsFromBuffer crash, "call to OpenGL ES API with no current context"

I want to save a view's snapshot to a file, but comes the error.
My code is as following:
View decor = ***; //
int width = decor.getWidth();
int height = decor.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
decor.draw(canvas);
int bytes = bitmap.getByteCount() + 8;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
buffer.putInt(width);
buffer.putInt(height);
bitmap.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
but bitmap.copyPixelsToBuffer(buffer); will crash.
the error is like this:
12-09 08:36:43.107: E/libEGL(14642): call to OpenGL ES API with no current context (logged once per thread)
This error only happens on Android 5.0, are there any changes in the new platform? I know android 5.0 use a ThreadedRender to render a surface, how can I handle this problem? Thanks very much!!!
Even i am facing the same problem! It seems like in pre-lollipop version the context was leaking and is being used by default (I assume). But with lollipop it is strictly needed to create or pass the context explicitly!
hope these helps!
https://stackoverflow.com/a/27092070

OutOfMemoryException in Image Resizing

We are using a .net dll (http://imageresizing.net/download) for imageresizing on runtime. It works perfectly. However, it happen after some time (between 1-7 days) that system started to raise exception in the even viewer:
Exception information:
Exception type: OutOfMemoryException
Exception message: Insufficient memory to continue the execution of the program.
And after that exception the website usually stop working with the error throwing "System.OutOfMemoryException".
And if we "recycle" the application pool in which the website is running, it clears the problem and website get back to normal immediately without any code change.
Before imagereiszing dll, we were using our custom code and same problem happen with that too. Following is the code.
private Bitmap ConvertImage(Bitmap input, int width, int height, bool arc)
{
if (input.PixelFormat == PixelFormat.Format1bppIndexed ||
input.PixelFormat == PixelFormat.Format4bppIndexed ||
input.PixelFormat == PixelFormat.Format8bppIndexed)
{
Bitmap unpackedBitmap = new Bitmap(input.Width, input.Height);
Graphics g = Graphics.FromImage(unpackedBitmap);
g.Clear(Color.White);
g.DrawImage(input, new Rectangle(0,0,input.Width, input.Height));
g.Dispose();
input = unpackedBitmap;
}
double aspectRatio = (double)input.Height / (double)input.Width;
int actualHeight = CommonMethods.GetIntValue(Math.Round(aspectRatio * width, 0));
Bitmap _imgOut;
if (actualHeight > height)
{
ResizeImage resizeImage = new ResizeImage(width, actualHeight, InterpolationMethod.Bicubic);
Bitmap _tempBitmap = resizeImage.Apply(input);
Bitmap _croppedBitmap = new Bitmap(width, height);
Graphics _crop = Graphics.FromImage(_croppedBitmap);
_crop.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
_crop.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
_crop.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
_crop.DrawImageUnscaledAndClipped(_tempBitmap, new Rectangle(0, 0, width, height));
_crop.Dispose();
_imgOut = _croppedBitmap;
}
else
{
ResizeImage resizeImage = new ResizeImage(width, height, InterpolationMethod.Bicubic);
_imgOut = resizeImage.Apply(input);
}
// Draw the arc if it has been requested
if (arc)
{
Graphics _arc = Graphics.FromImage(_imgOut);
_arc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
_arc.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
_arc.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
_arc.DrawArc(new Pen(Color.White, 24), new Rectangle(-13, -13, 50, 50), 180, 90);
_arc.Dispose();
}
// job done
return _imgOut;
}
We are resizing image like: www.mysite.com/images/myimage.jpg?width=196&height=131
Looking forward.
Farrukh
When you encounter an OutOfMemoryException (regardless of where it occurs), it can be caused by a memory leak anywhere in the application. Having debugged dozens of these instances with WinDbg, I've never found any that ended up being due to a bug in http://imageresizing.net.
That said, there's an easy way to determine whether it is a problem with http://imageresizing.net or not; create a separate application pool and subfolder application in IIS for your images and image resizing. Install nothing there except the image resizer. Next time you encounter the error, log on to the server and find out which w3wp.exe instance is responsible for the massive memory usage.
If it's in the ImageResizer app pool, go collect your $20-$50 bug bounty from http://imageresizing.net/support. If not, you need to figure out where you're leaking stuff in your main application.
If you're working with System.Drawing anywhere else in the app, that's the first place to look. Check your code against this list of pitfalls.
If you're positive you're disposing of every System.Drawing.* instance in a using or finally clause, then read this excellent article a few times to make sure you're not failing on any of the basics, then dig in with DebugDiag and/or WinDBG (see bottom of article).

Resources