I am developing a WPF C# video player with the MediaElement.
Some videos contain black bars at the top and bottom. I want to resize the picture i.e. to 120% so that no black bars are shown when I play these videos.
Now the question is how I can measure the size of the black bars. If i.e. a video has a height of 800 pixels I want to measure the height of the black bars. If there are 50 pixels black at the top and 50 pixels on the bottom then I will calculate that I have to resize the picture i.e. to 112%.
I guess I could make a screenshot of the video and then analyze that picture to find out how many black pixels are at the top and bottom (I have no idea how complicated that would be). But I guess and hope there is an easier way to do this. Any ideas?
This is the beginning of my answer: I just wrote a quick and dirty solution with the screenshot.
I used RenderTargetBitmap to create a screenshot with the height of the video but just 1 pixel wide. Then I used the BitmapSource.CopyPixels Method to extract the single pixels and then I looped through the pixels from top to bottom. It works, and it work fast enough for me (<30ms for the whole procedure).
I will publish the complete code when it’s done.
I am still interested if there is an easier way to do this. My own answer is just one answer but I am happy to accept something better as “the answer”.
Related
So this may be a bit vague, but I'm having some issues. Basically, I am drawing my little ninja sprite (16x16px) and on 2 frames of his animation, he loses pixels on his headband. I have looked everywhere in an attempt to find an answer, but to no avail.
Original Sprite Sheet (Bugged frames are 4th row, 3rd and 4th frames):
After a little more investigation, it seems that the bugged pixels are due to the width of the sprite sheet. It almost seems random what pixels get messed up depending on the width. However, with a constant width, the messed up pixels are constant regardless of creating a new build or using an old one.
As you can see, the back of his headband is missing pixels. While it may not be game changing, it is very annoying and if anyone knows of a fix, that'd be great! Thanks guys!
My guess is that those pixels are magenta (255,0,255) which is automatically keyed out of the image as transparent by the SpriteTextureProcessor. If that is the case, change the pixels to be a slightly different color (255,1,255) or use the TextureProcessor for the content. You can do this in the properties window of the image file in your content project.
So a workaround I found is actually changing the size of the rectangle used to display the image. It was set to 100, setting it to 99 works and there is barely any visual difference.
I have a treeview that contains icons. The icons are ugly. and by ugly i mean low quality. and by low quality i mean something i would expect to see from a dos program
i would hope there would be a way to improve the image quality of the icons, but after looking around on microsoft's development site, I have yet to find a solution.
to be honest, at this point i don't even know what to look for. 'image quality' is very too broad of a phrase to search for (i've gotten very random results from google searches).
i am using an ImageList to store these icons in the TreeView. there really isnt much code to show that would be of use here - at least i dont think so.
sorry for the boring question.
Is this what you're looking for?
http://msdn.microsoft.com/en-us/library/system.windows.forms.imagelist.colordepth.aspx
The default value is set to 8-bit, so changing this property of your list will probably help. Essentially, just add the following line to your code:
imagelist.ColorDepth = ColorDepth.Depth32Bit;
It can be improved. What you need to do is to open it in photoshop or gimp and resample it at 3 times it size. Then resample it at 600 dpi and resize it down to 8 x 10.
Notice, that I specified to resize and not down size. You click on the pic and the corners have a handle for you to click on and size it down to the appropraite size. This way, the pixels are closer together.
Then, you use a very soft blur, I mean really soft. The secret is when it is printed. We print at 600 to 1200 dpi which is what will enhance the picture and that is what a printer has to keep in mind. When you print on glossy paper at 600 dpi, the pic comes out really nice and sharp.
This may seem like a strange question, but I have a set of pictures I want to use as a fading screen saver, and I want each picture to have an accompanying quote, but each quote will be in a different place, relative to the picture.
Rather than coding a Rectangle for the area of each image, it would be easier just to draw a rectangle in the image, and have it drawn in there.
Now, the drawn shape would be a stark, uniform coloured border (lime green, for instance) because the colour doesn't appear in the pictures, and it would be on a solid coloured background, like black for instance.
My question is: If I draw a lime green rectangle on each image, how could I then, using C#, find that rectangle in the image, get the dimensions of it, and replace the lime green with the background colour, so in the end product, it'd look like the shape was never there to begin with?
I have not tried anything, I have no code to show, because it's an idea I had and though I'm sure it must be possible, I don't even know what to start searching for.
I hope this is possible, if it isn't, then I'll just draw a rectangle for each one, but that's a last resort. :)
Have you thought about using either the emgucv or aforge libraries? They are able to detect shapes in images quite easily. Though they don't detect edges perfectly, if all you want to detect is a rectangle, either one should work well.
Here are two tutorials on shape detection for emgu cv and aforge. Once you have the shape detected, then you can easily cut it out for the background. Say, for example, that you used the aforge Detection of quadrilaterals (in the link), then you could easily get the rectangles position and size by the calling of blobs[i].Rectangle; and create a rectangle with same size and position with picture background color.
If you need more clarification please feel free to comment. Nice idea!
I want to set an image to have a transparent background, but I do not want to replace all pixels of a specific colour with transparency.
To be more specific, the image is a thumbnail image for a folder, obtained via IShellItemImageFactory.GetImage. This gives me a Bitmap, as displayed in Windows Explorer thumbnail view, but the background is solid white.
I can use Bitmap.MakeTransparent on it, and that will work in most cases, but in any cases where the thumbnail image contains white itself (for example, a folder that contains images, which include white colours).
Incidently, this is the first time in over 10 years as a developer that, after googling my question, I have not found an answer anywhere, and I've actually had to ask it myself. (I think that means I just levelled up! Yippee, I am now a level 2 developer...)
Use flood-fill algorithm to fill pixels of the same color from the OUTSIDE as you need it. It is something similar to magic wand in photoshop.
http://en.wikipedia.org/wiki/Flood_fill
What I would do is flood-fill with some obscure color (Magenta always does it for me), then replace that color with transparent (I don't know if flood filling with transparent pixels is feasible).
So what you're getting from IShellItemImageFactory.GetImage is a composite image that contains the original image on a white background? I suspect the white background is there if the image doesn't have the same aspect ratio as the thumbnail size. For example, if you ask for a 96x96 thumbnail of a 640x480 image, there's going to be some white space at top and bottom.
If that's the case, you have a problem. You can't differentiate between white pixels that are contained in the image, and white pixels that are added by GetImage.
There are a few things you could do. You could load the image and resize it yourself. That's probably the easiest. You'd want to maintain your own thumbnail cache then.
Or you could examine the image returned by GetImage to look for white space on the sides. Basically, if every pixel on a row (or column) is white, then make that row (or column) transparent. It's a little more complicated than that (the NBA logo, for example). But that's essentially what you'd want to do.
I am creating a custom control using C# GDI+.
Quick explanation...the control will be say 500 pixels on screen but will contain perhaps 500000 pixels of information. So although i'm only showing 500px at a time i need to obviously scroll in the horizontal plane (left & right). The tricky part is that each 500px chunk of bitmap takes a while (between 100ms - 1000ms) to render.
So my plan is to maintain a 1500px bitmap in memory. i.e. the 500px visible part and 500px either side of the visible area and draw the off-screen parts asynchronously as the user scrolls.
I would like some feedback, advice, criticism or examples of code to help me achieve this. It seems fairly straight forward but after a few initial test attempts its proving more difficult than one would imagine.
Thanks.
The effectiveness of this approach depends on, amongst other things, the amount of moving around the user will do. If the user is making small movement, then stopping to consider the new information, this could work. However, if the user is whizzing back and forth, you'll still have an issue.
Does your application lend itself to gradually improving the quality of the image - i.e. providing a quick usable image and then improving it as the user stops to consider it?
I had a similar problem a few years back. As dommer mentions, if you're processing chunks of the image before displaying them you're best off showing something and improving it later. If you're having problems blitting the original image, you've got something wrong with your method. GDI+ is very particular about pixel depth (you want 32bpp with alpha).
In our case, we processed in 500px tiles and padded out a tile around the visible view Ff the user scrolled outside the area we'd processed we blitted bits of the original image with a dark semi-opaque rectangle super-imposed on them. These chunks were queued for processing. As we processed chunks of the image (centre-out) they semi-opaque rectangles would disappear.
It worked reasonably well, was very responsive and very fast. It's very fast to blit the original bitmap onto the screen, and in our case the processing was usually very close behind. The effect of the tiles getting lighter was actually quite pretty.
Draw only the visible area.
Build a method
Bitmap DrawGraph(leftMargin, rightMargin) {...}
and then in OnPaint() do
Bitmap bmp = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height);
bmp = DrawGraph(e.ClipRectangle.Left, e.ClipRectangle.Right);
e.Graphics.DrawImageUnscaled(bmp, e.ClipRectangle.Location);