Any way to block-copy part of picture in WPF? - c#

I'm looking for analog of BitBlt functionality, which was available in WinAPI.

Check out method BitmapSource.CopyPixels (it also has one more overload).

Where do you want to copy that part to? Another image?
In WPF, you create a Graphics context on an empty Image, then you draw the Image into this Graphics context, where you can specify parameters such as the location and width/height of the source image to draw.

Related

Transparent images not showing properly [duplicate]

I've got this topimage with alpha channel in it and I need to put this image over another background image, while the alpha channel from the top image stays intact obviously.
Now I've seen some tutorials with Canvas, but my project doesn't seem to recognize Canvas.
Anyone got an idea why I cant use Canvas or how to put those 2 images over each other?
Ok, I will try to answer: after loading the image, like this more or less, pseudocode:
Bitmap bmp = new Bitmap("MyCooolSemiTransparentImage.png");
bmp.MakeTransparent(colorHaveToBeRenderedTransparent);
colorHaveToBeRenderedTransparent is a color wich results non transparent after loading it into Bitmap object.
EDIT
if alphachannel is ok, here is a simple tutorial how to draw in image on WinForms:
msdn: DrawImage
Call method provided in yuor forms OnPaint override and you will get what you want.
Hope this helps.
Regards.

Set image offset of ImageBrush?

I've got a large image in memory which I convert to an System.Windows.Media.ImageBrush and use it as the Fill for a System.Windows.Shapes.Rectangle. You can move this rectangle around with your cursor.
Basically I want to use the rectangle as a "viewport". Thus I need to change which parts of the image get displayed within the rectangle, i.e., define a rectangular subsection of the image.
How can I do that?
I see ImageBrush.Viewport but that doesn't seem to mean the same thing.
I'm open to alternative solutions that don't involve a rectangle, such as drawing directly on a canvas or something, but AFAIK WPF doesn't let you access pixel data directly (at least not easily).
To achieve this your going to have to create your own rectangle user control to allow the user to create/resize a rectangle. Then I would create a CroppedBitmap of the image in the rectangle portion Cropped Bitmap MSDN Stackoverflow example
Edit
No, no, no #Mark, You dont turn the CroppedBitmap into a UserControl. You create a USerControl that exposed the CroppedBitmap. Basically, you create a UserControl with the following DependencyProperties
The Image
The Width of he cropped portion
The Height of the cropped portion
The Left of the cropped portion
Top of the cropped portion
Then as soon as any of these properties your DP callback will do a RenderTargetBitmap Crop of the new region.

How do I get the dimensions of the ImageRectangle in PictureBox?

Background
I want to be able to get the drawn dimensions of a zoomed image inside the picturebox (I'll explain below).
The PictureBox.ImageRectangle property seems to be exactly what I'm looking for because it shows the height and width of the resized image, as well as it's relative top, left position inside the control.
The trouble is PictureBox.ImageRectangle is private and so I can't read the values without using reflection (which is obviously not ideal).
Actual Question
My question is, is there another way that I can easily get to these values without writing a method to calculate what the values "ought" to be? I can do that easily enough, but I feel I'd be reinventing the wheel.
Context:
I'm writing a simple image processing app in C# and one of the things it has to do is allow the user to draw a selection around a portion of the image (a lot like the Marquee tool in Photoshop).
I need to know the dimensions of the rendered image so I know where to set the bounds of my marquee tool and also to translate the values of the drawn rectangle to points on the scaled bitmap inside the control.
My answer look simple so maybe I'm missing something, but I think Control.DisplayRectangle suits your need.
EDIT
OK, missed the point; however see How to get the value of non- public members of picturebox?
if you want to access dimension of the image in picture box you can use
GraphicsUnit units = GraphicsUnit.Point;
RectangleF imgRectangleF = pictureBox1.Image.GetBounds(ref units);
Rectangle imgRectangle = Rectangle.Round(imgRectangleF);

How do you draw a string to a Bitmap in Silverlight?

In normal C# it is easy to draw to a bitmap using the Grpahics.DrawString() method. Silverlight seems to have done away with Bitmap objects and Graphics is no longer available either. So...How am I meant to manipulate/create a bitmap when using Silverlight? If it helps, I am using Silverlight 3.
Let me tell you what I am doing. I am being given a template, basically a pre-rendered image. The user is then able to select from multiple images and enter the deisred text. I then render it to the image, adjusting size etc... within bounds and centering it in the pre-defined area of the image. If I can calculate the size (as in the MeasureString method) and then draw the string (as in the Graphics.DrawString method) that would be fine. The real question, no matter why I want to be able to do this, is can it be done?
The question is: why do you want to? Why not just use a TextBlock?
If you are trying to dynamically generate an image, use standard Silverlight/WPF controls (including TextBlock) and render them to a WritableBitmap.
Edit:
Ok, you've updated and expanded, which gives me more to go on. Unfortunately, you're not going to like the answer. First, keep in mind that Silverlight and WPF in general are vector based, and intended to be used as such. Although the Canvas allows you to do pseudo-pixel manipulations, you cannot be nearly as pixel-accurate as old-school GDI. This is a factor of your medium. If you absolutely have to measure things the way you want to measure them, I suggest you build your images on a remote server and transmit them to your Silverlight app.
You can calculate the size on-screen of the text rendered via a TextBlock using the ActualWidth and ActualHeight properties. But it only works on an already rendered control. Something like MeasureString is simply not available in Silverlight. Based on your description of your app, some user interaction could accomplish what you want. The user selects the image, enters the text, and is shown a preview. The user can then adjust the width and height of the various text areas until satisfied, at which point you can take a snapshot using the render method I liked to above.
The following may work, its a bit nebulous because I haven't tried yet myself.
The object you are looking for is the WritableBitmap.
You create a Visual tree, for example create your self a Grid or Canvas (you're not adding this to the UI). Add to it the selected image and a TextBlock positioned and sized as you prefer.
Create a new WritableBitmap either of a specific size or using the selected image to initialize it.
Use the WritableBitmap Render method passing the above root Grid or Canvas to it.
Now you have a bitmap which you should able to use to do whatever its you needed to do that required all this hoop jumping in the first place.

.Net & C#: Trying to have a transparent image on a button (assigned from IDE)

Using VS2005 and C#.
Having a button in a form and an image from a resource, the image does not have transparency.
How can I have transparency when assigning the image from the IDE ?
Thank you.
Open the image in an image editor (Paint.NET and GIMP are free) and add the transparencies wherever you need to.
It will all work once the image actually has transparent pixels.
You can also use a couple methods of the Bitmap class to do this:
Bitmap b = Properties.Resources.MyImage;
b.MakeTransparent(b.GetPixel(0, 0));
I don't really understand what you are asking. You can use an image with transparency on a button as long as the image type you are using supports transparency - such as .png.
Edit: I read your question again and it is still confusing, but maybe you meant to say that you want to add transparency to the image? If so, you would have to use an image editor to add the transparency and save it in a format that supports this. Paint.Net is a good free tool for this.

Categories