I'm attempting to convert a image to bitmap so that i can make it rotate based on angles received through the serial port, and then output the resultant image to a pictureBox. however this results in either a build failure or the image wont show on the screen. i am using the AForge Imaging.Filters framework to achieve this. current code:
private void imageRotate(int angle)
{
int angleToMove = angle; //- lastAngle;
Bitmap image = Properties.Resources.Compass_Transparent;
pictureBox1.Image = null;
RotateBilinear ro = new RotateBilinear(angleToMove, true);
ro.FillColor = Color.White;
pictureBox1.BackColor = Color.White;
Bitmap image2 = ro.Apply(image);
pictureBox1.Image = image2;
lastAngle = angle;
}
i am able to achieve the desired effect by using
Bitmap image = (Bitmap)Image.FromFile(#"path");
and i can currently set the pictureBox to the image initially by using
img = TableTurner.Properties.Resources.Compass_Transparent;
pictureBox1.Image = img;
however this does not work when trying to reference the resources file.
Related
I have worked on image capture window application. When I have captured image by application in window tablet then image quality low and show darkness in captured image background. When I have captured image by tablet then image is good quality.
What is missing/problem in my code?
I have used code share by you...
private void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
ImgContainer.Image = b;
}
private void btnKeep_Click(object sender, EventArgs e)
{
int width = 457;
int height = 350;
Image tmpimg = ImgContainer.Image;
System.Drawing.Bitmap b = new System.Drawing.Bitmap(ImgContainer.Image, width, height);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(b);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);
System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
string ImagePath = Guid.NewGuid().ToString();
string imagefullpath = System.AppDomain.CurrentDomain.BaseDirectory + "imageFolder\\" + ImagePath + ".jpg";
b.Save(imagefullpath);
}
Show you captured by application image ............
Show you captured by tablet ............
Please give me any idea and solution remove darkness captured by application (above image).
You can use a DrawImage with an ImageAttributes instance to change the gamma. I found 0.5f to work:
Here is a function that applies a gamma value to a bitmap and returns a modified bitmap. It is up to you to ..:
make sure you don't leak resources
make sure to apply the gamma always to the original and not repeatedly to the same bitmap when giving the user a trackbar to find a good value..
The function:
public static Bitmap ApplyGamma(Bitmap bmp0, float gamma)
{
Bitmap bmp1 = new Bitmap(bmp0.Width, bmp0.Height);
using (Graphics g = Graphics.FromImage(bmp1))
{
ImageAttributes attributes = new ImageAttributes();
attributes.SetGamma(gamma, ColorAdjustType.Bitmap);
g.DrawImage(bmp0, new Rectangle(0, 0, bmp0.Width, bmp0.Height),
0, 0, bmp0.Width, bmp0.Height, GraphicsUnit.Pixel, attributes);
}
return bmp1;
}
The calling code I used:
Image img = Image.FromFile(yourImage); // some image to use
float gamma = (float)(trackBar1.Value / 10f); // a trackbar to test
Text = "Gamma = " + gamma; // a control display
pictureBox1.Image = ApplyGamma((Bitmap)img, gamma);
If you also want to change contrast and/or brightness you can use a ColorMatrix. See here for an example!
I am taking an image with Kinect in C# and want to display it on the next page of the application.
I can acquire the image successfully and can store it successfully on disk since I'm able to send this image as an email attachment.
The photo acquisition code is:
public void CaptureScreen(double x, double y, double width, double height)
{
int ix, iy, iw, ih;
ix = Convert.ToInt32(x);
iy = Convert.ToInt32(y);
iw = Convert.ToInt32(width);
ih = Convert.ToInt32(height);
// set the kinect hand icon invisible
kinectButton.Visibility = System.Windows.Visibility.Collapsed;
kinectButton2.Visibility = System.Windows.Visibility.Collapsed;
Bitmap image = new Bitmap(iw, ih,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(image);
g.CopyFromScreen(ix, iy, ix, iy,
new System.Drawing.Size(iw, ih),
CopyPixelOperation.SourceCopy);
image.Save("../../Images/image_display.png");
desk_input();
}
I use the image_display.png file to display the image on the screen.
I'm using the following code for displaying the image:
private void desk_input() {
BitmapImage bi2 = new BitmapImage();
bi2.BeginInit();
bi2.UriSource = new Uri("/Images/image_display.png", UriKind.RelativeOrAbsolute);
bi2.EndInit();
photo_preview.Visibility = System.Windows.Visibility.Visible;
photo_preview.Source = bi2;
Canvas.SetLeft(photo_preview, 750);
Canvas.SetTop(photo_preview, 400);
}
However, during run time, the photo taken when the application was run last time keeps being displayed instead of the current photo. I'm guessing this is because the image_display.png file is added to the binary when the application is compiled.
So can you suggest a way where I can display the image taken with the kinect immediately?
Edit: I solved the problem by refreshing the bitmap cache as follows:
private void desk_input() {
BitmapImage bi2 = new BitmapImage();
bi2.BeginInit();
bi2.UriSource = new Uri("/Images/image_display.png", UriKind.RelativeOrAbsolute);
bi2.CacheOption = BitmapCacheOption.OnLoad; //Changed - Reload the file
bi2.EndInit();
photo_preview.Visibility = System.Windows.Visibility.Visible;
photo_preview.Source = bi2;
Canvas.SetLeft(photo_preview, 750);
Canvas.SetTop(photo_preview, 400);
}
I've drawn ellipse over canvas, now how can I save it as an image. I know you cannot directly save canvas as an image and neither you can take screenshot. I am working in C#/xaml. Below is my code for drawing ellipse over canvas.
private void canvasDraw_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (drawing)
{
PointerPoint current = e.GetCurrentPoint((UIElement)sender);
// Line line = new Line() { X1 = start.Position.X, Y1 = start.Position.Y, X2 = current.Position.X, Y2 = current.Position.Y };
//line.Stroke = new SolidColorBrush(Colors.Black);
Ellipse circle = new Ellipse();
circle.SetValue(Canvas.LeftProperty, current.Position.X);
circle.SetValue(Canvas.TopProperty, current.Position.Y);
circle.Height = 20;
circle.Width = 20;
circle.Fill = currentBrush;
circle.Opacity = 0.7;
circle.SetValue(Canvas.ZIndexProperty,1);
canvasDraw.Children.Add(circle);
}
}
Edit : I am able to save the image by using InkManager. I stored every Ellipse in inkmanager and called SaveAsync method but the final issue is the image comes in black for example if I draw red ellipse the saved image has black ellipse.
Read this example http://blogs.msdn.com/b/swick/archive/2007/12/02/rendering-ink-and-image-to-a-bitmap-using-wpf.aspx
Code Extract from site:
// render InkCanvas' visual tree to the RenderTargetBitmap
RenderTargetBitmap targetBitmap =
new RenderTargetBitmap((int)inkCanvas1.ActualWidth,
(int)inkCanvas1.ActualHeight,
96d, 96d,
PixelFormats.Default);
targetBitmap.Render(inkCanvas1);
// add the RenderTargetBitmap to a Bitmapencoder
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
// save file to disk
FileStream fs = File.Open(fileName, FileMode.OpenOrCreate);
encoder.Save(fs);
Yes RenderTargetBitmap it is not available in windows store app target to 8.0
Windows 8.1 APIs include the new RenderTargetBitmap class that allows to render to a bitmap with its RenderAsync methods.
you can now use this method
link although its not working with MediaElement which i need :(
I found code to rotate an image. My problem is that when I save it, the image is already opened, and in use by me (As, I opened it to rotate it).
How can I avoid this?
public static void RotateImage(string filePath, float angle)
{
//create a new empty bitmap to hold rotated image
using (var img = Image.FromFile(filePath))
{
using(var bmp = new Bitmap(img.Width, img.Height))
{
//turn the Bitmap into a Graphics object
Graphics gfx = Graphics.FromImage(bmp);
//now we set the rotation point to the center of our image
gfx.TranslateTransform((float) bmp.Width/2, (float) bmp.Height/2);
//now rotate the image
gfx.RotateTransform(angle);
gfx.TranslateTransform(-(float) bmp.Width/2, -(float) bmp.Height/2);
//set the InterpolationMode to HighQualityBicubic so to ensure a high
//quality image once it is transformed to the specified size
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
//now draw our new image onto the graphics object
gfx.DrawImage(img, new Point(0, 0));
//dispose of our Graphics object
}
img.Save(filePath);
}
edit: Updated code as per Anthony's advice.
edit:
Just some FYI, this was accomplished in a couple of lines...
public static void RotateImage(string filePath, float angle)
{
//create a new empty bitmap to hold rotated image
byte[] byt = System.IO.File.ReadAllBytes(filePath);
var ms = new System.IO.MemoryStream(byt);
using (Image img = Image.FromStream(ms))
{
RotateFlipType r = angle == 90 ? RotateFlipType.Rotate90FlipNone : RotateFlipType.Rotate270FlipNone;
img.RotateFlip(r);
img.Save(filePath);
}
}
Using your existing code you can do the following:
byte[] byt = System.IO.File.ReadAllBytes(filepath);
System.IO.MemoryStream ms = new System.IO.MemoryStream(byt);
Image img = Image.FromStream(ms);
That will not have the file locked when you go to save it.
I have an image I display on my website. Which is written in c#. I want to give my user the ability to click on a button which rotates the image. This will rotate the actual image on the server so next time it is displayed it is displayed the correct way.
Similar to how facebook has image rotation?
Do you really need to rotate the image on the server? Why not just store a property with the image which stores the rotation value like 90, 180, 270... and apply this every time image is retrieved and update/save the property value once user rotates the image
see this tutorial for how to rotate an image or google it you will find a lot of samples
//Create Image element
Image rotated270 = new Image();
rotated270.Width = 150;
//Create source
BitmapImage bi = new BitmapImage();
//BitmapImage properties must be in a BeginInit/EndInit block
bi.BeginInit();
bi.UriSource = new Uri(#"pack://application:,,/sampleImages/watermelon.jpg");
//Set image rotation
bi.Rotation = Rotation.Rotate270;
bi.EndInit();
//set image source
rotated270.Source = bi;
public static Image RotateImage(Image image, Size size, float angle)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
if (size.Width < 1 || size.Height < 1)
{
throw new ArgumentException("size must be larger than zero.");
}
Bitmap tempImage = new Bitmap(size.Width, size.Height);
using (Graphics tempGraphics = Graphics.FromImage(tempImage))
{
PointF center = new PointF((float)size.Width / 2F, (float)size.Height / 2F);
tempGraphics.TranslateTransform(center.X, center.Y, MatrixOrder.Prepend);
tempGraphics.RotateTransform(angle != 180F ? angle : 182F/*at 180 exact angle the rotate make a small shift of image I don't know why!*/);
tempGraphics.TranslateTransform(-center.X, -center.Y, MatrixOrder.Prepend);
tempGraphics.DrawImage(image, new PointF());
}
return tempImage;
}