private void btnCapture_Click(object sender, RoutedEventArgs e)
{
//WriteableBitmap bitmap = new WriteableBitmap(target , null );
WriteableBitmap bitmap = new WriteableBitmap(500, 500);
//
if (bitmap != null)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "JPEG Files (*.jpeg)|*.jpeg";
saveDlg.DefaultExt = ".jpeg";
if ((bool)saveDlg.ShowDialog())
{
using (Stream fs = saveDlg.OpenFile())
{
SaveToFile(bitmap, fs);
// SaveToFile(bitmap);
MessageBox.Show("Screenshot Saved");
}
}
}
}
private static void SaveToFile(WriteableBitmap bitmap,Stream fs)
// private static String SaveToFile(WriteableBitmap bitmap)
{
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int bands = 3;
byte[][,] raster = new byte[bands][,];
//Convert the Image to pass into FJCore
for (int i = 0; i < bands; i++)
{
raster[i] = new byte[width, height];
}
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
int pixel = bitmap.Pixels[width * row + column];
raster[0][column, row] = (byte)(pixel >> 16);
raster[1][column, row] = (byte)(pixel >> 8);
raster[2][column, row] = (byte)pixel;
}
}
ColorModel model = new ColorModel {colorspace = ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
//Encode the Image as a JPEG
MemoryStream stream = new MemoryStream();
FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
encoder.Encode();
//Back to the start
stream.Seek(0, SeekOrigin.Begin);
//Get the Bytes and write them to the stream
byte[] binaryData = new Byte[stream.Length];
long bytesRead;
bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
fs.Write(binaryData, 0, binaryData.Length );
// string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
// return base64String;
}
// took this code from a blog(as i was having no proper idea for getting screenshot)
after writing WriteableBitmap bitmap = new WriteableBitmap(target , null); it is showing an error i.e
System.ArgumentNullException was unhandled by user code Message=Value cannot be null. Parameter name: element StackTrace: at System.Windows.Media.Imaging.WriteableBitmap..ctor(UIElement element, Transform transform) at TutorialViewChangeEvents.MainPage.btnCapture_Click(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.onclick() at System.Windows.Controls.Button.onclick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags) InnerException:
But after commenting that and using WriteableBitmap bitmap = new WriteableBitmap(500, 500); it is not showing an error and saves the image, but pixel width and height is accoding to what i enter(i.e 500,500 here).
But it is showing black image i.e not the actual.
What should i do now?

New Topic/Question
Reply


MultiQuote




|