Select an image from rectangle using writeablebitmap - c#

I have written an application in silverlight, I am placing a rectangle on the image and want to select the part of image covered by rectangle and show it on a image control on click of a button.
I am not good at handling ratios and image manipulation things, so I am unable to get it right way.
The code for the same goes as below, and would appreciate, if anyone could suggest me a way or solution to get around with this.
public void CaptureImage(object sender, RoutedEventArgs e)
{
BitmapImage bitmapImage = new BitmapImage();
//// bitmapImage.CreateOptions = BitmapCreateOptions.None;
bitmapImage = NewImage;
////calculate bounding box
int originalWidth = bitmapImage.PixelWidth;
int originalHeight = bitmapImage.PixelHeight;
int newSmallWidth = (int)SquareBlue.Width;
int newSmallHeight = (int)SquareBlue.Height;
////generate temporary control to render image
Image temporaryImage = new Image { Source = bitmapImage, Width = newSmallWidth, Height = newSmallHeight };
////create writeablebitmap
WriteableBitmap wb = new WriteableBitmap(newSmallWidth, newSmallHeight);
TranslateTransform t = new TranslateTransform();
t.X = -5;
t.Y = -5;
wb.Render(temporaryImage, t);
wb.Invalidate();
myImage.Source = wb;
}
Whenever this code gets executed, whole image gets snapped, instead of the part selected by rectangle. Could anyone, guide me as what I am doing wrong here.

I'd recommend that you use the Crop method the WriteableBitmapEx library provides.

Related

Load image in Picture Box when User Control shows to user

I want to show a lot of images in a form using User Control, but it's very slow and takes a lot of system memory.
How can I load image in Picture Box when User Control shows to user?
I wrote this sample code. It works fine for 700 images, but I am still looking for best solution.
Bitmap myBitmap;
Image myThumbnail;
foreach (var item in queryMatchingFiles)
{
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
myBitmap = new Bitmap(item);
myThumbnail = myBitmap.GetThumbnailImage(120, 183, myCallback, IntPtr.Zero);
flowLayoutPanel1.Controls.Add(new PictureBox {
Image = myThumbnail,
Width = 120,
Height = 183,
BackgroundImageLayout = ImageLayout.Stretch
});
count++;
myBitmap = null;
myThumbnail = null;
}
public bool ThumbnailCallback()
{
return false;
}

original picture within cropped picture?

i'm currently trying to crop an image, then send the crop image from the preview to a pictureBox but the pictureBox is showing both the original and the cropped combine.
im learning cropping from the Microsoft sample "CSWinFormCropImage". MS linke
here is the code i have been editing.
public partial class MainForm : Form
{
Boolean bHaveMouse;
Point ptOriginal = new Point();
Point ptLast = new Point();
Rectangle rectCropArea;
Image srcImage = null;
public MainForm()
{
InitializeComponent();
bHaveMouse = false;
}
private void BtnCrop_Click(object sender, EventArgs e)
{
TargetPicBox.Size = new Size(rectCropArea.Width, rectCropArea.Height);
TargetPicBox.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image, SrcPicBox.Width, SrcPicBox.Height);
using (Graphics g = Graphics.FromImage(sourceBitmap))
{
g.DrawImage(sourceBitmap, new Rectangle(0, 0, TargetPicBox.Width, TargetPicBox.Height),
rectCropArea, GraphicsUnit.Pixel);
}
TargetPicBox.Image = sourceBitmap;
}
i have not edited any of the other code that controls the cropping tool. cause i think my issue is with BtnCrop_click and targetPicBox.Image = sourceBitmap.

Capture the screen selected by Current Form in WPF?

I am creating a application which can capture the screen which is selected by current active windows form and make user aware by setting it as the background of it. Please refer the image
But my problem is i cant get the size of the active window size. This is the code i have been working on
private void button5_Click(object sender, RoutedEventArgs e)
{
Image b = null;
int w = (int)this.Width;
int h = (int)this.Height;
**System.Drawing.Size sz = this.Size;
System.Drawing.Point loc = this.Location;**
Hide();
System.Threading.Thread.Sleep(500);
using (b = new Bitmap(w, h))
{
using (Graphics g = Graphics.FromImage(b))
{
g.CopyFromScreen(loc, new System.Drawing.Point(0, 0), sz);
}
Image x = new Bitmap(b);
ImageBrush myBrush = new ImageBrush();
x.Save(#"C:\Users\DZN\Desktop\testBMP.jpeg", ImageFormat.Jpeg);
myBrush.ImageSource = new BitmapImage(new Uri(#"C:\Users\DZN\Desktop\testBMP.jpeg", UriKind.Absolute));
this.Background = myBrush;
}
Show();
}
In the bolded lines i get the error saying WpfApplication1.MainWindow' does not contain a definition for 'Size, location. but this works well in windows forms. Any help is hugly appreciated. Thank you.
WPF Window doesn't have a size property instead you can use ActualWidth and ActualHeight. Same way it doesn't exposes Location also but you can use Left and Top properties.
All the above properties are of type double so you need a cast to appropriate type.
System.Drawing.Size sz = new System.Drawing.Size((int)ActualWidth, (int)ActualHeight);
System.Drawing.Point loc = new System.Drawing.Point((int)Left, (int)Top);

How to make picturebox transparent?

I am making an application in C# .NET. I have 8 picture boxes in it. I used PNG images with transparent background but in my form it is not transparent when it comes above another image.
I am using Visual Studio 2012. This is a screenshot of my form:
One way to do this is by changing the parent of the overlapping picture box to the PictureBox over which it is lapping. Since the Visual Studio designer doesn't allow you to add a PictureBox to a PictureBox, this will have to be done in your code (Form1.cs) and within the Intializing function:
public Form1()
{
InitializeComponent();
pictureBox7.Controls.Add(pictureBox8);
pictureBox8.Location = new Point(0, 0);
pictureBox8.BackColor = Color.Transparent;
}
Just change the picture box names to what ever you need. This should return:
GameBoard is control of type DataGridView;
The image should be type of PNG with transparent alpha channel background;
Image test = Properties.Resources.checker_black;
PictureBox b = new PictureBox();
b.Parent = GameBoard;
b.Image = test;
b.Width = test.Width*2;
b.Height = test.Height*2;
b.Location = new Point(0, 90);
b.BackColor = Color.Transparent;
b.BringToFront();
Try using an ImageList
ImageList imgList = new ImageList;
imgList.TransparentColor = Color.White;
Load the image like this:
picturebox.Image = imgList.Images[img_index];
I've had a similar problem like this.
You can not make Transparent picturebox easily such as picture that shown at top of this page, because .NET Framework and VS .NET objects are created by INHERITANCE! (Use Parent Property).
I solved this problem by RectangleShape and with the below code I removed background,
if difference between PictureBox and RectangleShape is not important and doesn't matter, you can use RectangleShape easily.
private void CreateBox(int X, int Y, int ObjectType)
{
ShapeContainer canvas = new ShapeContainer();
RectangleShape box = new RectangleShape();
box.Parent = canvas;
box.Size = new System.Drawing.Size(100, 90);
box.Location = new System.Drawing.Point(X, Y);
box.Name = "Box" + ObjectType.ToString();
box.BackColor = Color.Transparent;
box.BorderColor = Color.Transparent;
box.BackgroundImage = img.Images[ObjectType];// Load from imageBox Or any resource
box.BackgroundImageLayout = ImageLayout.Stretch;
box.BorderWidth = 0;
canvas.Controls.Add(box); // For feature use
}
One fast solution is set image property for image1 and set backgroundimage property to imag2, the only inconvenience is that you have the two images inside the picture box, but you can change background properties to tile, streched, etc. Make sure that backcolor be transparent.
Hope this helps
Just use the Form Paint method and draw every Picturebox on it, it allows transparency :
private void frmGame_Paint(object sender, PaintEventArgs e)
{
DoubleBuffered = true;
for (int i = 0; i < Controls.Count; i++)
if (Controls[i].GetType() == typeof(PictureBox))
{
var p = Controls[i] as PictureBox;
p.Visible = false;
e.Graphics.DrawImage(p.Image, p.Left, p.Top, p.Width, p.Height);
}
}
you can set the PictureBox BackColor proprty to Transparent

Load an image from media library onto canvas with WriteableBitmap image?

I am trying to load a picture selected from media library of windows phone and I have done upto choosing the required picture and I am unable to load image onto my canvas named area with this code :
void photochoosertask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
WriteableBitmap bitMap = new WriteableBitmap(200,200);
Extensions.LoadJpeg(bitMap, e.ChosenPhoto);
Canvas.SetLeft(area, 10);
Canvas.SetTop(area, 10);
bitMap.Render(area, null);
bitMap.Invalidate();
}
}
But I am unable to do with this code..any suggestions..??
or how to do this task ? Is this the correct way ?
Thanks
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bi = new BitmapImage();
bi.SetSource(e.ChosenPhoto);
WriteableBitmap b = new WriteableBitmap(bi);
Image img = new Image();
img.Source = b;
Canvas.SetLeft(img, 10);
Canvas.SetTop(img, 10);
area.Children.Add(img);
}
In order to show the bitmap in your Canvas you would have to add an Image control to its Children collection, which uses the bitmap as its Source:
var bitmap = new WriteableBitmap(200, 200);
Extensions.LoadJpeg(bitmap, e.ChosenPhoto);
var image = new Image();
image.Source = bitmap;
Canvas.SetLeft(image, 10);
Canvas.SetTop(image, 10);
area.Children.Add(image);
As e.ChosenPhoto is a stream, you may perhaps also use a BitmapImage instead of a WriteableBitmap, and set its source stream to e.ChosenPhoto. You may then set the size of the Image control to the desired values.
var bitmap = new BitmapImage();
bitmap.SetSource(e.ChosePhoto);
var image = new Image();
image.Source = bitmap;
image.Width = 200;
image.Height = 200;
Canvas.SetLeft(image, 10);
Canvas.SetTop(image, 10);
area.Children.Add(image);

Categories