How to download PDF file into client system? - c#

I want to download a PDF file from server side into client system. A Report page is changed into PDF file and saved into a project folder that is on server side. Here the problem is that when I access it from client system and try to generate PDF file then I am not sure whether it has successfully generated a PDF file into server side project folder or not and it is not automatically downloaded into the client system. But when I run project from local system then it is working correctly.
Here I post my code, please check it and please give me a solution for this, I need it very much
My code is:
protected void btn_print_Click(object sender, EventArgs e)
{
try
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
int width = 850;
int height = 550;
Thumbnail1 thumbnail = new Thumbnail1(url, 990, 1000, width, height);
Bitmap image = thumbnail.GenerateThumbnail();
image.Save(Server.MapPath("~") + "/Dwnld/Thumbnail.bmp");
imagepath = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.bmp";
imagepath1 = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.pdf";
convetToPdf();
}
catch (Exception)
{
throw;
}
}
string imagepath = null;
string imagepath1 = null;
public void convetToPdf()
{
PdfDocument doc = new PdfDocument();
System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
PdfPage pdfPage = new PdfPage();
pdfPage.Orientation = PageOrientation.Landscape;
doc.Pages.Add(pdfPage);
// XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4)
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(imagepath);
xgr.DrawImage(img, 0, 0);
doc.Save(imagepath1);
xgr.Dispose();
img.Dispose();
doc.Close();
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = imagepath1;
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
}
public class Thumbnail1
{
public string Url { get; set; }
public Bitmap ThumbnailImage { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int BrowserWidth { get; set; }
public int BrowserHeight { get; set; }
public Thumbnail1(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
this.Url = Url;
this.BrowserWidth = BrowserWidth;
this.BrowserHeight = BrowserHeight;
this.Height = ThumbnailHeight;
this.Width = ThumbnailWidth;
}
public Bitmap GenerateThumbnail()
{
Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return ThumbnailImage;
}
private void GenerateThumbnailInteral()
{
WebBrowser webBrowser = new WebBrowser();
webBrowser.ScrollBarsEnabled = false;
webBrowser.Navigate(this.Url);
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (webBrowser.ReadyState != WebBrowserReadyState.Complete) System.Windows.Forms.Application.DoEvents();
webBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser webBrowser = (WebBrowser)sender;
webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
webBrowser.ScrollBarsEnabled = false;
this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
webBrowser.BringToFront();
webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds);
this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
}
}
protected void CreateThumbnailImage(object sender, EventArgs e)
{
}

One potential problem with this code is that you're writing to the same files for every request. If there are multiple requests at the same time, some of them might fail.
To solve this you could write to the response stream directly, i.e.
protected void btn_print_Click(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
int width = 850;
int height = 550;
Thumbnail1 thumbnail = new Thumbnail1(url, 990, 1000, width, height);
using (Bitmap image = thumbnail.GenerateThumbnail())
convertToPdf(image);
}
public void convertToPdf(Image image)
{
using (PdfDocument doc = new PdfDocument())
{
System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
PdfPage pdfPage = new PdfPage();
pdfPage.Orientation = PageOrientation.Landscape;
doc.Pages.Add(pdfPage);
using (XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]))
{
using (XImage img = XImage.FromGdiPlusImage(image))
{
xgr.DrawImage(img, 0, 0);
using (MemoryStream stream = new MemoryStream())
{
doc.Save(stream, false);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", stream.Length.ToString());
stream.WriteTo(Response.OutputStream);
}
}
}
}
Response.End();
}
Edit Modified answer to use using statements to release resources.

Related

How to fix download issues for some users using WebClient DownloadFileAsync?

I created a Windows Form Application in C# that users of my game modification can use to download updates automatically.
The 'Launcher', as I call it, uses WebClient to download the updates. But the first release of the mod is very big (2,7 GB zipped). The launcher works perfect for me and most users, but for some users the extraction of the zip file logs an error where the file is corrupted and not readable.
I searched already on stack, and it is possible that the file might be corrupted or truncated due to bad internet connection. But how do I build in a method that fix that problem?
//Start downloading file
using (WebClient webClient = new WebClient())
{
webClient.DownloadFileCompleted += new
AsyncCompletedEventHandler(Client_DownloadFileCompleted);
webClient.DownloadProgressChanged += new
DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
webClient.DownloadFileAsync(new Uri("http://www.dagovaxgames.com/api/downloads/+ patch.path), downloadPath);
}
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
//install the update
InstallUpdate();
}
private void InstallUpdate()
{
var file = currentPatchPath;
//get the size of the zip file
fileInfo = new FileInfo(file);
_fileSize = fileInfo.Length;
installBackgroundWorker = new BackgroundWorker();
installBackgroundWorker.DoWork += ExtractFile_DoWork;
installBackgroundWorker.ProgressChanged += ExtractFile_ProgressChanged;
installBackgroundWorker.RunWorkerCompleted += ExtractFile_RunWorkerCompleted;
installBackgroundWorker.WorkerReportsProgress = true;
installBackgroundWorker.RunWorkerAsync();
}
EDIT, just showing install code so that you know I am using a backgroundworker to extract the zip.
Common approach here is to download large file in small chunks and put them together on client after completion. Using this approach you can: 1. run few downloads in parallel and 2. in case of problem with network you don't have to download entire file again, just download incomplete chunks.
I faced a similar issue many years ago and created a subclass of WebClient that uses the DownloadProgressChanged event and a timer to abort downloads that get hung up and cancels the download smoother than the underlying internet transport layer does. The code also supports a callback to notify the calling code of progress. I found that sufficient to smoothly handle occasional hiccups downloading 1GB-ish files.
The idea of breaking your download into multiple pieces also has merit. You could leverage a library such as 7-Zip to both create the chunks and piece them back together (many compression libraries have that feature; I'm personally most familiar with 7-Zip).
Here's the code I wrote. Feel free to use and/or modify in any way that's helpful to you.
public class JWebClient : WebClient, IDisposable
{
public int Timeout { get; set; }
public int TimeUntilFirstByte { get; set; }
public int TimeBetweenProgressChanges { get; set; }
public long PreviousBytesReceived { get; private set; }
public long BytesNotNotified { get; private set; }
public string Error { get; private set; }
public bool HasError { get { return Error != null; } }
private bool firstByteReceived = false;
private bool success = true;
private bool cancelDueToError = false;
private EventWaitHandle asyncWait = new ManualResetEvent(false);
private Timer abortTimer = null;
private bool isDisposed = false;
const long ONE_MB = 1024 * 1024;
public delegate void PerMbHandler(long totalMb);
public delegate void TaggedPerMbHandler(string tag, long totalMb);
public event PerMbHandler NotifyMegabyteIncrement;
public event TaggedPerMbHandler NotifyTaggedMegabyteIncrement;
public JWebClient(int timeout = 60000, int timeUntilFirstByte = 30000, int timeBetweenProgressChanges = 15000)
{
this.Timeout = timeout;
this.TimeUntilFirstByte = timeUntilFirstByte;
this.TimeBetweenProgressChanges = timeBetweenProgressChanges;
this.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(MyWebClient_DownloadFileCompleted);
this.DownloadProgressChanged += new DownloadProgressChangedEventHandler(MyWebClient_DownloadProgressChanged);
abortTimer = new Timer(AbortDownload, null, TimeUntilFirstByte, System.Threading.Timeout.Infinite);
}
protected void OnNotifyMegabyteIncrement(long totalMb)
{
NotifyMegabyteIncrement?.Invoke(totalMb);
}
protected void OnNotifyTaggedMegabyteIncrement(string tag, long totalMb)
{
NotifyTaggedMegabyteIncrement?.Invoke(tag, totalMb);
}
void AbortDownload(object state)
{
cancelDueToError = true;
this.CancelAsync();
success = false;
Error = firstByteReceived ? "Download aborted due to >" + TimeBetweenProgressChanges + "ms between progress change updates." : "No data was received in " + TimeUntilFirstByte + "ms";
asyncWait.Set();
}
private object disposeLock = new object();
void MyWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (cancelDueToError || isDisposed) return;
long additionalBytesReceived = e.BytesReceived - PreviousBytesReceived;
PreviousBytesReceived = e.BytesReceived;
BytesNotNotified += additionalBytesReceived;
if (BytesNotNotified > ONE_MB)
{
OnNotifyMegabyteIncrement(e.BytesReceived);
OnNotifyTaggedMegabyteIncrement(Tag, e.BytesReceived);
BytesNotNotified = 0;
}
firstByteReceived = true;
try
{
lock (disposeLock)
{
if (!isDisposed) abortTimer.Change(TimeBetweenProgressChanges, System.Threading.Timeout.Infinite);
}
}
catch (ObjectDisposedException) { } // Some strange timing issue causes this to throw now and then
}
public string Tag { get; private set; }
public bool DownloadFileWithEvents(string url, string outputPath, string tag = null)
{
Tag = tag;
asyncWait.Reset();
Uri uri = new Uri(url);
this.DownloadFileAsync(uri, outputPath);
asyncWait.WaitOne();
return success;
}
void MyWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null) success = false;
if (cancelDueToError || isDisposed) return;
asyncWait.Set();
}
protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this.Timeout;
return result;
}
void IDisposable.Dispose()
{
lock (disposeLock)
{
isDisposed = true;
if (asyncWait != null) asyncWait.Dispose();
if (abortTimer != null) abortTimer.Dispose();
base.Dispose();
}
}
}
I fixed it using a combination of a BackgroundWorker and downloading in small chunks (following up mtkachenko's solution). It also checks if the length of the downloaded file is the same as the one on the server. Using that, it can continue the download where the connection was interupted.
private void DownloadPatch(Patch patch){
//using background worker now!
downloadBackgroundWorker = new BackgroundWorker();
downloadBackgroundWorker.DoWork += (sender, e) => DownloadFile_DoWork(sender, e, patch);
downloadBackgroundWorker.ProgressChanged += DownloadFile_ProgressChanged;
downloadBackgroundWorker.RunWorkerCompleted += DownloadFile_RunWorkerCompleted;
downloadBackgroundWorker.WorkerReportsProgress = true;
downloadBackgroundWorker.RunWorkerAsync();
}
private void DownloadFile_DoWork(object sender, DoWorkEventArgs e, Patch patch)
{
string startupPath = Application.StartupPath;
string downloadPath = Path.Combine(Application.StartupPath, patch.path);
string path = ("http://www.dagovaxgames.com/api/downloads/" + patch.path);
long iFileSize = 0;
int iBufferSize = 1024;
iBufferSize *= 1000;
long iExistLen = 0;
System.IO.FileStream saveFileStream;
// Check if file exists. If true, then check amount of bytes
if (System.IO.File.Exists(downloadPath))
{
System.IO.FileInfo fINfo =
new System.IO.FileInfo(downloadPath);
iExistLen = fINfo.Length;
}
if (iExistLen > 0)
saveFileStream = new System.IO.FileStream(downloadPath,
System.IO.FileMode.Append, System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);
else
saveFileStream = new System.IO.FileStream(downloadPath,
System.IO.FileMode.Create, System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);
System.Net.HttpWebRequest hwRq;
System.Net.HttpWebResponse hwRes;
hwRq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(path);
hwRq.AddRange((int)iExistLen);
System.IO.Stream smRespStream;
hwRes = (System.Net.HttpWebResponse)hwRq.GetResponse();
smRespStream = hwRes.GetResponseStream();
iFileSize = hwRes.ContentLength;
//using webclient to receive file size
WebClient webClient = new WebClient();
webClient.OpenRead(path);
long totalSizeBytes = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
int iByteSize;
byte[] downBuffer = new byte[iBufferSize];
while ((iByteSize = smRespStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
if (stopDownloadWorker == true)
{
autoDownloadReset.WaitOne();
}
saveFileStream.Write(downBuffer, 0, iByteSize);
long downloadedBytes = new System.IO.FileInfo(downloadPath).Length;
// Report progress, hint: sender is your worker
int percentage = Convert.ToInt32(100.0 / totalSizeBytes * downloadedBytes);
(sender as BackgroundWorker).ReportProgress(percentage, null);
}
}
As you can see, I report the progress, so that I have a working progress bar as well.
private void DownloadFile_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
statusTextLabel.Text = "Downloading updates for version " + currentDownloadingPatch + " (" + e.ProgressPercentage + "%)";
progressBar.Value = e.ProgressPercentage;
}

Image from a web page that doesn't stop loading

I'm looking to take a screenshot from a web page that is streaming from an IP camera and as such doesn't triggerWebBrowserReadyState.Complete.
The webpage in question is literally streaming a mjpeg and the browser is always loading/streaming.
I can generate images for other URLs but cannot guess this to give anything other than 'Navigation Cancelled'.
I have tried WebBrowser.Stop() but to no avail...
The webpage is simply an mJpeg:
<img class"shrink" src="192.168.1.124/Streaming/channels/1/httpPreview"; alt="192.168.1.124/Streaming/channels/1/httpPreview"; class="shrinkToFit" height="148" width="217"> </img>
using Telegram.Bot;
using System.Threading.Tasks;
using System.IO;
using Telegram.Bot.Types;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace Telegram_Alerter
{
public static class BitmapExtensions
{
public static void SaveJPG100(this Bitmap bmp, string filename)
{
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
}
public static void SaveJPG100(this Bitmap bmp, Stream stream)
{
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
}
public static ImageCodecInfo GetEncoder(ImageFormat format)
{
var codecs = ImageCodecInfo.GetImageDecoders();
foreach (var codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
// Return
return null;
}
}
class Program
{
static public void Main(string[] args)
{
TelegramMessageAsync().Wait();
}
static public async Task TelegramMessageAsync()
{
TelegramBotClient Bot = new TelegramBotClient("<mykey>");
WebsiteToImage websiteToImage = new WebsiteToImage("http://192.168.1.124/Streaming/channels/1/httpPreview", Application.StartupPath + "file.jpg");
websiteToImage.Generate();
using (FileStream fs = System.IO.File.OpenRead(Application.StartupPath + "file.jpg"))
{
FileToSend fts = new FileToSend();
fts.Content = fs;
fts.Filename = "file.jpg";
await Bot.SendPhotoAsync(<bot>, fts, "message here");
}
}
public class WebsiteToImage
{
private Bitmap m_Bitmap;
private string m_Url;
private string m_FileName = string.Empty;
public WebsiteToImage(string url)
{
// Without file
m_Url = url;
}
public WebsiteToImage(string url, string fileName)
{
// With file
m_Url = url;
m_FileName = fileName;
}
public Bitmap Generate()
{
// Thread
var m_thread = new Thread(_Generate);
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _Generate()
{
var browser = new WebBrowser { ScrollBarsEnabled = false };
browser.Navigate(m_Url);
browser.DocumentCompleted += WebBrowser_DocumentCompleted;
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
browser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Capture
var browser = (WebBrowser)sender;
browser.ClientSize = new Size(1024,768);
browser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(1024,768);
browser.BringToFront();
browser.DrawToBitmap(m_Bitmap, browser.Bounds);
// Save as file?
if (m_FileName.Length > 0)
{
// Save
m_Bitmap.SaveJPG100(m_FileName);
}
}
}
}
}
Maybe that Page contains Iframes that never load.If yes:
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// to check if it is not Iframe
if (e.Url.AbsolutePath != this.webBrowser.Url.AbsolutePath)
{
// Capture
var browser = (WebBrowser)sender;
browser.ClientSize = new Size(1024,768);
browser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(1024,768);
browser.BringToFront();
browser.DrawToBitmap(m_Bitmap, browser.Bounds);
// Save as file?
if (m_FileName.Length > 0)
{
// Save
m_Bitmap.SaveJPG100(m_FileName);
}
}
}
Thanks for the efforts but I answered the question by using an FFMpeg .NET wrapper called N.Reco meaning I did not have to worry about parsing the MJPEG to obtain a single frame and then convert to a JPEG.
This was achieved in two lines of code but FFMPEG does add 20Mb to your application.
var ffmpeg = new NReco.VideoConverter.FFMpegConverter();
ffmpeg.GetVideoThumbnail(pathToVideoFile, Application.StartupPath + stamp);

How to Save Record Video in SharpAvi

I would like to record screen video then save as Mp4,Webm or any video format.
I am using SharpAvi to record video,The code below is recording video but I am unable to save after stop ,Please help Here is my code.
namespace RecordScreenvideo
{
// Used to Configure the Recorder
public class RecorderParams
{
public RecorderParams(string filename, int FrameRate, FourCC Encoder, int Quality)
{
FileName = filename;
FramesPerSecond = FrameRate;
Codec = Encoder;
this.Quality = Quality;
Height = (int)SystemParameters.PrimaryScreenHeight;
Width = (int)SystemParameters.PrimaryScreenWidth;
}
string FileName;
public int FramesPerSecond, Quality;
FourCC Codec;
public int Height { get; private set; }
public int Width { get; private set; }
public AviWriter CreateAviWriter()
{
return new AviWriter(FileName)
{
FramesPerSecond = FramesPerSecond,
EmitIndex1 = true,
};
}
public IAviVideoStream CreateVideoStream(AviWriter writer)
{
// Select encoder type based on FOURCC of codec
if (Codec == KnownFourCCs.Codecs.Uncompressed) return writer.AddUncompressedVideoStream(Width, Height);
else if (Codec == KnownFourCCs.Codecs.MotionJpeg) return writer.AddMotionJpegVideoStream(Width, Height, Quality);
else
{
return writer.AddMpeg4VideoStream(Width, Height, (double)writer.FramesPerSecond,
// It seems that all tested MPEG-4 VfW codecs ignore the quality affecting parameters passed through VfW API
// They only respect the settings from their own configuration dialogs, and Mpeg4VideoEncoder currently has no support for this
quality: Quality,
codec: Codec,
// Most of VfW codecs expect single-threaded use, so we wrap this encoder to special wrapper
// Thus all calls to the encoder (including its instantiation) will be invoked on a single thread although encoding (and writing) is performed asynchronously
forceSingleThreadedAccess: true);
}
}
}
public class Recorder : IDisposable
{
#region Fields
AviWriter writer;
RecorderParams Params;
IAviVideoStream videoStream;
Thread screenThread;
ManualResetEvent stopThread = new ManualResetEvent(false);
#endregion
public Recorder(RecorderParams Params)
{
this.Params = Params;
// Create AVI writer and specify FPS
writer = Params.CreateAviWriter();
// Create video stream
videoStream = Params.CreateVideoStream(writer);
// Set only name. Other properties were when creating stream,
// either explicitly by arguments or implicitly by the encoder used
videoStream.Name = "Captura";
screenThread = new Thread(RecordScreen)
{
Name = typeof(Recorder).Name + ".RecordScreen",
IsBackground = true
};
screenThread.Start();
}
public void Dispose()
{
stopThread.Set();
screenThread.Join();
// Close writer: the remaining data is written to a file and file is closed
writer.Close();
stopThread.Dispose();
}
void RecordScreen()
{
var frameInterval = TimeSpan.FromSeconds(1 / (double)writer.FramesPerSecond);
var buffer = new byte[Params.Width * Params.Height * 4];
Task videoWriteTask = null;
var timeTillNextFrame = TimeSpan.Zero;
while (!stopThread.WaitOne(timeTillNextFrame))
{
var timestamp = DateTime.Now;
Screenshot(buffer);
// Wait for the previous frame is written
videoWriteTask?.Wait();
// Start asynchronous (encoding and) writing of the new frame
videoWriteTask = videoStream.WriteFrameAsync(true, buffer, 0, buffer.Length);
timeTillNextFrame = timestamp + frameInterval - DateTime.Now;
if (timeTillNextFrame < TimeSpan.Zero)
timeTillNextFrame = TimeSpan.Zero;
}
// Wait for the last frame is written
videoWriteTask?.Wait();
}
public void Screenshot(byte[] Buffer)
{
using (var BMP = new Bitmap(Params.Width, Params.Height))
{
using (var g = Graphics.FromImage(BMP))
{
g.CopyFromScreen(Point.Empty, Point.Empty, new Size(Params.Width, Params.Height), CopyPixelOperation.SourceCopy);
g.Flush();
var bits = BMP.LockBits(new Rectangle(0, 0, Params.Width, Params.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
Marshal.Copy(bits.Scan0, Buffer, 0, Buffer.Length);
BMP.UnlockBits(bits);
}
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FourCC selectedCodec = KnownFourCCs.Codecs.MotionJpeg;
RecorderParams recparams = new RecorderParams("recordvideo",49,selectedCodec,100);
Recorder record = new Recorder(recparams);
}
}
}
This is very simple to save output as mp4 or any format as you can see the method`receiving file name as first argument
RecorderParams recparams = new RecorderParams("recordvideo",49,selectedCodec,100);`
So you need to just pass your output file path as
RecorderParams recparams = new RecorderParams("D:\\recordvideo.mp4",49,selectedCodec,100);`
You just need to call the dispose method
so after you start recording you either need to hard-code how much time you want it to record or make it after you press any key, here is an example of main that will start record when you press any key then it will stop recording after you press any key.
private void button1_Click(object sender, EventArgs e)
{
FourCC selectedCodec = KnownFourCCs.Codecs.MotionJpeg;
RecorderParams recparams = new RecorderParams("recordvideo",49,selectedCodec,100);
Console.WriteLine("Press any key to start recording ");
Console.ReadKey();
Recorder record = new Recorder(recparams);
Console.WriteLine("Press any key to stop recording ");
Console.ReadKey();
record.Dispose();
}

A generic error occurred in GDI When Saveing image in Database

I've uploaded my web form application on my host. in a part of my application, i try to save the image and int's thumbnail in sql server database. the data type of fields im using to save is varbinary(max). when I test the application on my computer. it works good but after uploading I got this error when trying to save the image in database.
The code I've written is like this:
NowbakhtEntities entities = new NowbakhtEntities();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
if (ImageUpload.HasFile)
{
Byte[] contents = ImageUpload.FileBytes;
MemoryStream imgStream = new MemoryStream(contents);
Byte[] thumb = null;
System.Drawing.Image img1 = new Bitmap(imgStream);
if (img1.Height < img1.Width)
{
int x = Convert.ToInt32((150*img1.Height)/img1.Width);
System.Drawing.Image bmp2 = img1.GetThumbnailImage(150, x, null, IntPtr.Zero);
thumb = ImageToByte(bmp2);
bmp2.Dispose();
}
else
{
int x = Convert.ToInt32((150*img1.Width)/img1.Height);
System.Drawing.Image bmp2 = img1.GetThumbnailImage(x, 150, null, IntPtr.Zero);
thumb = ImageToByte(bmp2);
bmp2.Dispose();
}
Project project = new Project();
project.Title = txtTitle.Text;
project.Description = txtDesc.Content;
project.Image = contents;
project.Thumb = thumb;
entities.Projects.Add(project);
entities.SaveChanges();
img1.Dispose();
imgStream.Close();
Response.Redirect("ProjectList.aspx");
}
}
public byte[] ImageToByte(System.Drawing.Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

Download an image from the web and store in media library (windows phone 8)

i am trying to download an image from the web save it in the media library , below is my code, am i missing something here, Thanks in advance
public void storePicture()
{
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
string url = #"http://mynokiablog.com/wp-content/uploads/2012/11/wp8.jpeg";
BitmapImage storeimage = new BitmapImage(new Uri(url));
// height and width are 0
int testheight = storeimage.PixelHeight;
int testwidth = storeimage.PixelWidth;
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile("testname");
// NullRefrenceException
WriteableBitmap wb = new WriteableBitmap(storeimage);
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
add event handler as below
storeimage.ImageOpened += bitmapImage_ImageOpened;
storeimage.ImageFailed += bitmapImage_ImageFailed;
storeimage.DownloadProgress += bitmapImage_DownloadProgress;
then in bitmapImage_DownloadProgress, create WritableBitMap and save
private void LoadIMG()
{
var bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
bitmapImage.ImageOpened += bitmapImage_ImageOpened;
bitmapImage.ImageFailed += bitmapImage_ImageFailed;
bitmapImage.DownloadProgress += bitmapImage_DownloadProgress;
bitmapImage.UriSource = new Uri("http://ds.serving-sys.com/BurstingRes///Site-16990/Type-0/7b912e70-352a-454f-8ea7-5d5ecd6ebfae.gif");
}
private void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
{
}
private void bitmapImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
}
private void bitmapImage_ImageOpened(object sender, RoutedEventArgs e)
{
var userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
var writeableBitmap = new WriteableBitmap(sender as BitmapImage);
var isolatedStorageFileStream = userStoreForApplication.CreateFile("Myfile.gif");
writeableBitmap.SaveJpeg(isolatedStorageFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
}
You can try it: http://www.kunal-chowdhury.com/2012/02/how-to-download-and-save-images-in-wp7.html#.TzUtWBSaLkM.twitter
Hope this helps.

Categories

Resources