在安卓端发送链接申请到PC端,可以使用网络请求库(如HttpClient)发送HTTP请求到PC端的服务器,请求包含链接申请的信息。

在PC端,可以创建一个简单的服务器应用程序,监听来自安卓端的请求。当收到请求时,可以通过按钮点击事件处理程序,将PictureBox的内容转换为图像文件,然后将图像文件发送回安卓端。

下面是一个简单的示例,演示了如何实现安卓端发送链接申请到PC端,再通过按钮将PictureBox的内容发送回安卓端展示。

安卓端代码(使用C#和HttpClient库发送链接申请):

using System;
using System.Net.Http;
using System.Threading.Tasks;

public class AndroidClient
{
    private readonly HttpClient httpClient;

    public AndroidClient()
    {
        httpClient = new HttpClient();
    }

    public async Task SendLinkRequest(string link)
    {
        var request = new HttpRequestMessage(HttpMethod.Post, "http://pc-server-ip:port/link-request");
        request.Content = new StringContent(link);

        var response = await httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();
    }
}

PC端代码(使用C#和ASP.NET创建简单的服务器应用程序):

using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Windows.Forms;

public class PcServer
{
    private readonly HttpListener httpListener;
    private PictureBox pictureBox;

    public PcServer(PictureBox pictureBox)
    {
        this.pictureBox = pictureBox;

        httpListener = new HttpListener();
        httpListener.Prefixes.Add("http://*:port/link-request/");
    }

    public async Task StartListening()
    {
        httpListener.Start();

        while (true)
        {
            var context = await httpListener.GetContextAsync();
            var request = context.Request;
            var response = context.Response;

            if (request.HttpMethod == "POST" && request.Url.AbsolutePath == "/link-request")
            {
                var link = await new StreamReader(request.InputStream).ReadToEndAsync();
                Console.WriteLine($"Received link request: {link}");

                // 将PictureBox的内容转换为图像文件
                var image = pictureBox.Image;
                var imagePath = "image.jpg";
                image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                // 发送图像文件到安卓端
                await SendImage(imagePath, response);
            }
        }
    }

    private async Task SendImage(string imagePath, HttpListenerResponse response)
    {
        response.ContentType = "image/jpeg";
        response.ContentLength64 = new FileInfo(imagePath).Length;
        response.Headers.Add("Access-Control-Allow-Origin", "*");

        using (var stream = response.OutputStream)
        {
            using (var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
            {
                await fileStream.CopyToAsync(stream);
            }
        }

        response.Close();
    }
}

在PC端的Windows Forms应用程序中,可以使用以下代码启动服务器并处理按钮点击事件:

using System;
using System.Threading.Tasks;
using System.Windows.Forms;

public class MainForm : Form
{
    private AndroidClient androidClient;
    private PcServer pcServer;
    private PictureBox pictureBox;
    private Button sendButton;

    public MainForm()
    {
        androidClient = new AndroidClient();

        pictureBox = new PictureBox();
        pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
        // 设置PictureBox的位置和大小

        sendButton = new Button();
        sendButton.Text = "发送图片";
        sendButton.Click += SendButton_Click;
        // 设置sendButton的位置和大小

        Controls.Add(pictureBox);
        Controls.Add(sendButton);
    }

    private async void SendButton_Click(object sender, EventArgs e)
    {
        // 发送链接申请到PC端
        await androidClient.SendLinkRequest("link");

        // 启动PC端服务器
        pcServer = new PcServer(pictureBox);
        await pcServer.StartListening();
    }
}

上述示例代码仅提供了一个简单的实现思路,具体的实现方式可能因项目需求和环境而有所不同。

C# 安卓端发送链接申请到PC端,并接收PictureBox图片展示

原文地址: https://www.cveoy.top/t/topic/qfed 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录