Image manipulation in C# - c#

I want to copy an image with all its parameters, including the manipulation data (scale, rotatio etc).
I'm creating a new image control and copy the source and size but can't find a way to set the same manipulation data.
I'm using System.Windows.Controls.Image and i have the ManipulationData object of the source image.
Have any idea?
Thanks

There's very good nuget package available for this..
https://www.nuget.org/packages?q=Tags%3A%22Magick.NET%22
For example to crop or resize an image:
MagickImage image = new MagickImage("filepath.jpg");
image.Crop(new MagickGeometry(424, 448, 224, 224));
image.Resize(123,234);
image.Write("output.jpg");

You can use RenderTargetBitmap to render any UIElement to bitmap:
The class exists in both, WPF and Windows Store Apps, however the usage is slightly different.
WPF Links:
- https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap(v=vs.110).aspx
Windows Store Links:
https://msdn.microsoft.com/library/windows/apps/dn298548
http://social.technet.microsoft.com/wiki/contents/articles/20648.using-the-rendertargetbitmap-in-windows-store-apps-with-xaml-and-c.aspx#Use_its_Pixels

Related

SoftwareBitmap compared to BitmapImage

So I'm creating a UWP app which includes an InkCanvas, as such, I want to store the contents as a variable so that I can show a preview of the canvas in the menu.
My question is, what is the best bitmap to use, as there appear to be a number of options.
The SoftwareBitmap is included in Windows.Graphics.Imaging, whereas the BitmapImage is part of Windows.UI.XAML.Media.Imaging, both of which are available in a UWP app.
Can the UWP Image class for displaying images use either of these formats?
Which is most appropriate in my case?
So having done much experimenting, it seems for use with the Image control from Windows.UI.XAML.Controls, a SoftwareBitmap works fairly well.
A SoftwareBitmapSource can be assigned to the Image.Source, provided the SoftwareBitmap has BitmapPixelFormat.Bgra8 and BitmapAlphaMode.Premultiplied (or BitmapAlphaMode.None). From the Remarks section on the SoftwareBitmapSource page:
A SoftwareBitmap displayed in a XAML app must be in BGRA pixel format with pre-multiplied alpha values
All works nicely, now to work on scaling the bitmap down as the difference in size between the InkCanvas and Image make the thumbnail/preview look poor.

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.

Cropping an image stored locally

I'm kind of a greenhorn to Windows Phone development and I've been looking for a way in order to crop an image already built into the project (Maybe even from the camera some day), but every package I've found has either had a fuss with Visual Studio, or throws argument exceptions. So I've decided that I will make my own function to do so.
However, I have not the slightest idea where to start. I'm pretty sure WriteableBitmap has something to do with it, and something to do with the following code:
Application.GetResourceStream(new Uri("/PhoneApp3;component/Assets/Flowers/Daff.jpg"));
So how would one start out with getting the pixel data or creating a new image and apply pixel data. And finally how would one save the result and reference it through the UI's image elements.
Use WriteableBitmapEx to crop images on Windows Phone. The way you solution will have to work is to manipulate the WritableBitmap.Pixels property. You first load in an image, change the Pixels property and transform the raw pixels into a saved image format like JPG or PNG. That's a lot of work so lucky for you WriteableBitmapEx does that for you.
First, install WritebleBitmapEx from NuGet:
Install-Package WriteableBitmapEx
Then you can load any image, crop it and save back to the MediaLibrary. Here's for example how to load a file from the app's XAP, crop to top-left 25% of the image and save to the "Saved Pictures" WP7/8 album.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var bmp = new WriteableBitmap(0, 0).FromContent("Assets/ApplicationIcon.png");
var croppedBmp = bmp.Crop(0, 0, bmp.PixelWidth/2, bmp.PixelHeight/2);
croppedBmp.SaveToMediaLibrary("myImage.jpg");
}
When we run this code we can see the new cropped image:
I recently needed to do this and didn't want to use an external lib. Microsoft provide a good example on MSDN on how to do it (and is also very good at not causing memory leaks!)
http://code.msdn.microsoft.com/wpapps/Photos-Sample-a38a2c8e
Cheers,
Will

windows phone image overlay

I want to know how to overlay one image over the over in Windows Phone. I did some search online, a lot of people suggest put two images into one grid and adjust the margin. However, my case is a little different. I'm making the background image of the secondary tile, I want to integrate the two images, store it locally and make the tile. So I can't put them into a grid. So what should I do in this case? Thanks!
You can create UserControl with any layout you wish (173x173px). Then, when you need to generate a tile, put this control to a page (probably out of the screen) and make and image from it with new WriteableBitmap(YourTile, null);. Than save these image to the /Shared/ShellContent/ and you are done
Probably there are better solutions for this task but this works fine too
I made this work using Ku6opr method using the following
WriteableBitmap bmp = new WriteableBitmap(173, 173);
bmp.Render(renderRoot, new TranslateTransform());
bmp.Invalidate();
where renderRoot is the usercontrol containing the Grid and the Image. I then save the bmp to the /Shared/ShellContent folder in Isolated storage.

.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