3

ショートカットキーを押すと、画面の完全なスクリーンショットをアップロードできる小さな個人用画面キャプチャアプリを作成しようとしています。

ファイルを自分の Web サイトにアップロードすることはできましたが、問題は、URL にアクセスすると壊れた画像として表示されることです。

これが私のコードです:

private void CaptureFullScreen()
{
    string file = DateTime.Now.ToString("ddmmyyyyhhmmss") + ".jpg";
    string file_store = screenshotDir + "\\" + file;

    Rectangle bounds = Screen.GetBounds(Point.Empty);
    using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using(Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
        }

        bitmap.Save(file_store, ImageFormat.Jpeg); 
    }

    //System.Diagnostics.Process.Start(file);
    ShowBalloonTip("Uploading...", "Screen Capture is being uploaded", ToolTipIcon.Info, 1000);
    FtpFileUpload(file_store, file);
}
private void FtpFileUpload(string file_store, string file_name)
{
    try
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://passion4web.co.uk/www/apps/imgcap/" + file_name);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential("username", "password");

        StreamReader strRead = new StreamReader(file_store);
        byte[] fileContents = Encoding.UTF8.GetBytes(strRead.ReadToEnd());
        strRead.Close();
        request.ContentLength = fileContents.Length;

        Stream reqStream = request.GetRequestStream();
        reqStream.Write(fileContents, 0, fileContents.Length);
        reqStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        string url = "http://passion4web.co.uk/apps/imgcap/" + file_name;
        string resp = response.StatusDescription;

        ShowBalloonTip("Screenshot uploaded", "Click this balloon to open", ToolTipIcon.Info, 5000, url);

        response.Close();
    }
    catch (Exception ex)
    {
        //Ignore this - used for debugging
        MessageBox.Show(ex.ToString(),"Upload error");
        MessageBox.Show(file_name + Environment.NewLine + file_store, "Filename, Filestore");
    }
}

以下に例を示します: スクリーンショット

何か案は?

4

1 に答える 1

3

これが問題です:

StreamReader strRead = new StreamReader(file_store);
byte[] fileContents = Encoding.UTF8.GetBytes(strRead.ReadToEnd());

UTF-8 でエンコードされたテキストであるかのようにファイルを読んでいます。そうではありません - それはイメージです。任意のバイナリ データ。

使用する:

byte[] fileContents = File.ReadAllBytes(file_store);

そして、すべてが大丈夫なはずです。

コードの残りの部分は、命名規則の修正、usingステートメントの適切な使用など、いくつかの TLC で行うことができますが、ここでの主な問題は、任意のバイナリ データをテキストとして扱うことです。

于 2012-11-03T23:54:59.133 に答える