Does any body know how to get a thumbnail/snapshot of a specific frame of a smooth streaming file using C#.net and WPF.
Regards,
Allan
Here MyPanel is the container where your video is streaming.
var panelPoint = this.MyPanel.PointToScreen(new Point(this.MyPanel.ClientRectangle.X, this.MyPanel.ClientRectangle.Y));
using (var bitmap = new Bitmap(320, 240))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(320, Point.Empty, new Size(320, 240));
}
if (SimpleIoc.Default.ContainsCreated<ICommonApplicationData>())
{
var imageGuidName = Guid.NewGuid();
fileName = Path.Combine("C:\", "TestFolder", imageGuidName + ".jpg");
bitmap.Save(fileName, ImageFormat.Jpeg);
var tempBitmapImage = new BitmapImage();
tempBitmapImage.BeginInit();
tempBitmapImage.UriSource = new Uri(fileName);
tempBitmapImage.EndInit();
image.Source = tempBitmapImage;
}
}
Related
I have an application that showing the pictures uploading from the system. For that I have to add watermark string into the images while uploading. When adding watermark to large size images, I receive “Parameter is not valid” when create image object from a memory stream.
What is the reason for this? Is there any other way to add watermark for large images.
using (FileStream fs = new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read))
{
using (Stream output = new MemoryStream())
{
using (Image myImage = Image.FromStream(fs, false, false))
{
Font font = new Font(fontType, fontSize.Equals(0) ? 10 : fontSize, (FontStyle)fontStyle, GraphicsUnit.Pixel);
Color color = ColorTranslator.FromHtml(string.IsNullOrEmpty(fontColor) ? "#000000" : fontColor);
Point pt = new Point(10, 5);
SolidBrush brush = new SolidBrush(color);
using (Bitmap img = new Bitmap(new Bitmap(myImage)))
{
using (Graphics graphics = Graphics.FromImage(img))
{
graphics.DrawString(waterMarkText, font, brush, pt);
img.Save(output, GetImageFormatToDownload(sourceFile.Extension.ToUpper().Substring(1, sourceFile.Extension.Length - 1)));
using (Image imgFinal = Image.FromStream(output))
{
using (var bmp = new Bitmap(img.Width, img.Height, img.PixelFormat))
{
using (var finalGraphics = Graphics.FromImage(bmp))
{
finalGraphics.DrawImage(imgFinal, 0, 0, bmp.Width, bmp.Height);
bmp.Save(Path.Combine(storePath, storeName), GetImageFormatToDownload(sourceFile.Extension.ToUpper().Substring(1, sourceFile.Extension.Length - 1)));
sourceFile = new FileInfo(Path.Combine(storePath, storeName));
}
}
}
}
}
}
}
}
Thanks.
I am trying to enlarge the size of screenshot without losing quality (as possible), but I can not do this. I am processing this picture in another method and filestream stop working. Actually tesseract can not read because of the screenshot's size so I am trying to enlarge the size of screenshot but I can not change the size of screenshot during capturing.
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(4000);
Snapshot().Save("D:\\program_goruntusu.jpg");
string s = FotoAnaliz();
}
private Bitmap Snapshot()
{
Bitmap Screenshot = new Bitmap(20, 20);
Graphics GFX = Graphics.FromImage(Screenshot);
GFX.CopyFromScreen(1243, 349, 0, 0, new Size(20, 20));
return Screenshot;
}
private string FotoAnaliz()
{
FileStream fs = new FileStream("D:\\program_goruntusu.jpg", FileMode.OpenOrCreate);
//string fotopath = #"D:\\program_goruntusu.jpg";
Bitmap images = new Bitmap(fs);
using (var engine = new TesseractEngine(#"./tessdata", "eng"))
{
engine.SetVariable("tessedit_char_whitelist", "0123456789");
// have to load Pix via a bitmap since Pix doesn't support loading a stream.
using (var image = new Bitmap(images))
{
using (var pix = PixConverter.ToPix(image))
{
using (var page = engine.Process(pix))
{
sayı = page.GetText();
MessageBox.Show(sayı);
fs.Close();
}
}
}
}
return sayı;
}
i've created a wpf class library dll to apply an shader effect to a System.Drawing.Bitmap. A method of this class will be called from a "normal" non wpf windows application async callback method (TCPServer send method).
The method will give me back a Bitmap and seems to working withou any issues. When i will call the Bitmap.Save(...) method i will get a "generic error in gdi+" error. Can anyone see what i've doing wrong?
The WPF Class Library DLL:
public static System.Drawing.Bitmap ApplyShaderToBitmap(System.Drawing.Bitmap Source, String FilenamePS)
{
System.Drawing.Bitmap returnBitmap = null;
Thread STAThread = new Thread(() =>
{
try
{
returnBitmap=Source;
double WPF_DPI_X = 96.0;
double WPF_DPI_Y = 96.0;
PixelShader ps = new PixelShader();
ps.UriSource = new Uri(FilenamePS);
LenseCorrectionEffect lce = new LenseCorrectionEffect(ps);
//Finally, apply the shader effect to img.
Image img = new Image();
img.Stretch = Stretch.None;
img.Effect = lce;
Viewbox viewbox;
viewbox = new Viewbox();
viewbox.Stretch = Stretch.None;
viewbox.Child = img;
img.BeginInit();
img.Width = Source.Width;
img.Height = Source.Height;
BitmapSource xx= BitmapToBitmapSource.ToBitmapSource(Source);
img.Source = xx;
img.EndInit();
viewbox.Measure(new Size(img.Width, img.Height));
viewbox.Arrange(new Rect(0, 0, img.Width, img.Height));
viewbox.UpdateLayout();
using (MemoryStream outStream = new MemoryStream())
{
//PngBitmapEncoder encoder = new PngBitmapEncoder();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
//RenderTargetBitmap bitmap = new RenderTargetBitmap((int)(img.Width * Source.DpiX / WPF_DPI_X), (int)(img.Height * Source.DpiY / WPF_DPI_Y), Source.DpiX, Source.DpiY, PixelFormats.Pbgra32);
RenderTargetBitmap bitmap = new RenderTargetBitmap(Convert.ToInt32(img.Width), Convert.ToInt32(img.Height), Convert.ToInt32(WPF_DPI_X), Convert.ToInt32(WPF_DPI_Y), PixelFormats.Pbgra32);
bitmap.Render(viewbox);
BitmapFrame frame = BitmapFrame.Create(bitmap);
encoder.Frames.Add(frame);
encoder.Save(outStream);
returnBitmap = new System.Drawing.Bitmap(outStream);
encoder = null;
bitmap = null;
frame = null;
}
xx = null;
viewbox = null;
img = null;
lce = null;
ps = null;
}
catch (Exception ex)
{
returnBitmap = null;
}
});
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.Start();
STAThread.Join();
STAThread = null;
return returnBitmap;
} // ApplyShaderToBitmap
The Calling from a async callback method:
bm = LenseCorrection.Shader.ApplyShaderToBitmap(bm, ShaderFilename);
bm.Save(ms, jgpEncoder, myEncoderParameters);
Thanks forward
I have WPF and C# application. which captures the images and Save in to file(*.jpg).
I have the image path and i want to rotate image saved in File through the c# code.
and Save the Rotated image in same file.
How can i do that?
Use the rotate flip method.
E.g.:
Bitmap bitmap1 = (Bitmap)Bitmap.FromFile(#"C:\test.jpg");
bitmap1.RotateFlip(RotateFlipType.Rotate180FlipNone);
bitmap1.Save(#"C:\Users\Public\Documents\test rotated.jpg");
you can use my method:
BitmapImage rotateImage(string filename,int angle)
{
WIA.ImageFile img = new WIA.ImageFile();
img.LoadFile(filename);
WIA.ImageProcess IP = new WIA.ImageProcess();
Object ix1 = (Object)"RotateFlip";
WIA.FilterInfo fi1 = IP.FilterInfos.get_Item(ref ix1);
IP.Filters.Add(fi1.FilterID, 0);
Object p1 = (Object)"RotationAngle";
Object pv1 = (Object)angle;
IP.Filters[1].Properties.get_Item(ref p1).set_Value(ref pv1);
img = IP.Apply(img);
File.Delete(filename);
img.SaveFile(filename);
BitmapImage imagetemp = new BitmapImage();
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
imagetemp.BeginInit();
imagetemp.CacheOption = BitmapCacheOption.OnLoad;
imagetemp.StreamSource = stream;
imagetemp.EndInit();
}
return imagetemp;
}
usage:
string filename = System.AppDomain.CurrentDomain.BaseDirectory + "4.jpg";
image.Source = rotateImage(filename,90);
I have one pictureBox and a button on form1 one. When the button is clicked, it should upload the file to the server. For now I am using the below method. First save the image locally and then upload to the server:
Bitmap bmp = new Bitmap(this.form1.pictureBox1.Width, this.form1.pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
Rectangle rect = this.form1.pictureBox1.RectangleToScreen(this.form1.pictureBox1.ClientRectangle);
g.CopyFromScreen(rect.Location, Point.Empty, this.form1.pictureBox1.Size);
g.Dispose();
bmp.Save("filename", ImageFormat.Jpeg);
And then uploading that file:
using (var f = System.IO.File.OpenRead(#"F:\filename.jpg"))
{
HttpClient client = new HttpClient();
var content = new StreamContent(f);
var mpcontent = new MultipartFormDataContent();
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
mpcontent.Add(content);
client.PostAsync("http://domain.com/upload.php", mpcontent);
}
I can't use the Bitmap type in StreamContent. How can I stream the image from pictureBox directly instead saving it as file first?
I came up with the below code using MemoryStream, but the uploaded file size is 0 using this method. Why?
byte[] data;
using (MemoryStream m = new MemoryStream())
{
bmp.Save(m, ImageFormat.Png);
m.ToArray();
data = new byte[m.Length];
m.Write(data, 0, data.Length);
HttpClient client = new HttpClient();
var content = new StreamContent(m);
var mpcontent = new MultipartFormDataContent();
content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
mpcontent.Add(content, "file", filename + ".png");
HttpResponseMessage response = await client.PostAsync("http://domain.com/upload.php", mpcontent);
//response.EnsureSuccessStatusCode();
string body = await response.Content.ReadAsStringAsync();
MessageBox.Show(body);
}
I am not sure if it is the correct way to do it, but I have solved it by creating a new stream and then copying the older one to it:
using (MemoryStream m = new MemoryStream())
{
m.Position = 0;
bmp.Save(m, ImageFormat.Png);
bmp.Dispose();
data = m.ToArray();
MemoryStream ms = new MemoryStream(data);
// Upload ms
}
Image returnImage = Image.FromStream(....);