Image not displayed correctly when moved - c#

I have an image <Image x:Name="image" Source="fingerprint.png" Height="1000"/>.
The image is higher than the form, so it doesn't fit in it completely.
When I move the image by image.Margin = new Thickness(0, image.Margin.Top - 50, 0, image.Margin.Bottom + 50);, the part of the image that wasn't visible before is still not visible, although it's now in the form (see red arrow).

Putting the image in a canvas solved the problem.

The Margin of the image will only control how close to the edges of the parent control your image is allowed to be.
I'm not sure what you are trying to achieve, but if you want the bottom of your image to be visible you need to either change its Position or scale it to fit inside the parent control.
Check this http://www.wpftutorial.net/LayoutProperties.html link to see some visualizations of layouting in WPF.
http://wpf.2000things.com/2011/04/14/276-change-image-sizing-using-the-stretch-property/ has some explanations about how to scale an image.

Related

How to display scaled image without anti-aliasing?

Question
How can I scale an image in XAML quickly without anti-aliasing applied?
Background
I am trying to make a pixel editor as a Windows 8 XAML/C# app. I'm using c#/XAML because most of my experience is with c#/WPF.
Method 1: WriteableBitmap + Image control. Originally, I used a WriteableBitmap to store and edit an image. That image is displayed in a resized XAML Image control. The problem is that the image does not get scaled properly because of anti-aliasing. (XAML does not seem to provide the BitmapScalingOptions that are available in WPF)
Method 2: Redrawn WriteableBitmap + Image control. Next I tried writing my own scaling, where I take my original image and write to a larger WriteableBitmap so that the scaled image is pixelated. The scaled image is then presented inside a XAML Image control. This process is slow and inefficient.
Method 3: SharpDX + Direct2d ?? I am pretty sure a solution exists somewhere between SharpDx, SurfaceImageSource, and Direct2d. Event still, I can't quite figure out how to display a SharpDx.WIC.Bitmap inside a Windows.UI.Xaml Image control, and am generally getting lost in the documentation.
What exactly is a recommended setup for achieving my desired end? Are there c# samples available which might point me in the right direction?
I don't know if you mean by scaling it by resizing it with the mouse or just with a button click to auto scale to a predetermined size.
But the thing you can do is to use an Image panel and then, just set it to image.Stretch = Stretch.Fill; so if you override and create a method for the Resize event of the Image Panel, your image will take the size to fill the Image panel.

Cannot make watermark image for Signature

I have a logo image that I need to embed in the signature. The logo is such like, a square with 4 parts & each of different part. I want to set watermark on the image, so the text is visible clearly. The code that I use is :
sap.Image = logoSign;
sap.ImageScale = 0.40f;
sap.Image.SetAbsolutePosition(100, 100);
//sap.Image.Transparency.SetValue(50, 0); // Only half image is visible
sap.GetAppearance().AddImage(logoSign);
It is scaled & placed properly. To achieve watermark effect, I add Transparency. When I made its value as (30,0) top left part of the square logo was visible. On making it 50, left half is visible, then I tried with 80, 100, 120, but the full logo is never visible. Elther 1/4th or 1/2 is only visible. The right part that is not showing up has Pink & yellow shades. Surprising is, when I comment the Transparency line, whole logo is visible.
Can anyone help me know, why is it acting like this. And ya, the logo with GIF is only working. PNG &/or JPG logo doesn't show up. Why so ?? I tried other image of jpg also, but no success.
#mkl, RESULTS shown in screenshots
Thanks
My advice would be to edit the image itself to a particular transparency level with the help of an editor and then use it. why do it in the code.

Overlay of two images - Control which part is in the foreground with mouse coursor

I need to be able to compare two different Images in the following way:
The images are displayed on top of each other so first there is only one image visible. When hovering with the mouse coursor over the image this defines the X-position of a vertical line which is splitting the image, displaying part of the first image left to the line and the rest of the second image right to the line.
This basically should be used as a quality comparison for two images with identical contents.
Here is a picture that hopefully makes my intentions clear:
you can use splitter control. splitter one side you put one picture control another side put other picture control
I was able to resolve this by using a SplitContainer and custom drawing. As described in the comment of Vasanthakumar's answer purely using a picturebox is not enough as you will not be able to align the right image at the same starting point than the left image (top left of the form).
What I did was the following:
pictureBox 1 displays its image normally
the Image in pictureBox 2 is drawn on every move of the splitter (this.splitContainer1.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.splitContainer1_SplitterMoved); with a custom subset of the Image to be displayed.
This effectively allows to generate the overlay I need.
Part of my implementation showing the drawing logic:
Bitmap bmp = new Bitmap(gImg2.Width, gImg2.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(gImg2, 0, 0, new Rectangle(e.SplitX, 0, gImg2.Width - e.SplitX, gImg2.Height), GraphicsUnit.Pixel);
}
pictureBox2.Image = bmp;

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 you get the REAL position of objects in silverlight?

How do you get the REAL position of objects in silverlight?
I have a header image centered on the screen. When I make the browser window smaller, obviously, the header's left side goes off the screen. Finding out the actual position is good to know if you want to position objects on top of the image.
I capture the Content_Resized and I run a little test:
if (App.Current.Host.Content.ActualWidth > header.Width)
{
TEST = Canvas.GetLeft(header);
}
else
{
TEST = Canvas.GetLeft(header);
}
TEST always returns zero.
EDIT: header sits on a grid instead of a canvas. "Well, there is your problem..." So a better question might be this. How would I get the margins of an image sitting on a grid?
I probably should just answer the question but how to find the position of an element relative to another is probably something that has been answered before (by myself and others) here and elsewhere on the tinternet.
However if your goal is to place an item over an image then place the image in a Grid and then add the item as child of the Grid. That way you assign the relative position over the image as the margin of the item and let Silverlight's layout system do the rest.
As a general rule if you feel that you need to write code to move stuff about when the size of things change then unless you are writing a custom panel or something you're probably not using Silverlight layout system properly.
Edit:
Try this experiment:-
<Grid x:Name="LayoutRoot">
<Grid x:Name="headerContainer" Margin="50, 60, 0, 0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Image Source="YourLargeImage" />
<Image Source="YourSmallerImage" HorizontalAlignment="Center" VerticalAlignment="Top" />
</Grid>
</Grid>
Now try changing the inner grid's Margin to move its position around the screen. Note the smaller image always remains at the top center of the large image.
I got it working.
First of all, these images are on a grid, not a canvas. But switching the grid to a canvas caused lots of other problems one of which is that I could not have the header image centered like before.
The solution was to change the margin of the smaller image sitting on top of the larger header image when the content resized like this:
blankbarimage.Margin = new Thickness((App.Current.Host.Content.ActualWidth - header.Width) / 2, 0, 0, 0);
and, by the way, you create a content resized method like this:
App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
So, to answer my own question, the way you get the REAL position of object in silverlight is (if they are on a grid) by looking at their margin settings.

Categories