【发布时间】:2015-02-07 12:16:34
【问题描述】:
这是我在这里的第一篇文章。我需要一些帮助来解决一些奇怪的系统。基本上我所拥有的是嵌入在我的游戏中的注册/用户注册,并且我已经为使用渲染目标的用户建立了一个遵循 128x128 像素大小的化身的系统。
https://www.dropbox.com/s/5vw8i9151uz6zkn/Screenshot%202014-12-08%2021.33.11.png?dl=0
我有一个在线系统,它应该接收图像数据,将其保存到文件中,然后将图像分配给已登录的用户。但是,当我发送图像数据时,它会保存到文件中很好,但由于某种原因不是正确的 png 文件。我使用以下代码将渲染目标保存到客户端的正确图像文件中
public static void SaveToGLPng(this Microsoft.Xna.Framework.Graphics.Texture2D texture, Stream s)
{
byte[] imageData = new byte[4 * texture.Width * texture.Height];
texture.GetData<byte>(imageData);
//Since OpenGL is a special snowflake, switch around the R and B values
for (int i = 0; i < imageData.Length; i += 4)
{
byte temp = imageData[i]; //store r
imageData[i] = imageData[i+2];//swap r
imageData[i+2] = temp;//swap b
}
int bp = 0;
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(texture.Width, texture.Height);
System.Drawing.Imaging.BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, texture.Width, texture.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pnative = bmData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(imageData, 0, pnative, 4 * texture.Width * texture.Height);
bitmap.UnlockBits(bmData);
bitmap.Save(s, System.Drawing.Imaging.ImageFormat.Png);
}
}
为了一切正常,我将它返回的数据保存到内存流中,并将其传递给一个名为 WebCall 的类,该类实现了 Async WebClient 功能
为 WebCall 调用的类
public void MakeCall()
{
using (WebClient wb = new WebClient())
{
UriBuilder b = new UriBuilder(url);
wb.UploadValuesCompleted += wb_UploadValuesCompleted;
wb.UploadValuesAsync(b.Uri, paramSet);
}
}
构造方法
public static WebCall UploadAvatar(byte[] data, int userID)
{
WebCall call = new WebCall();
call.url = URL;
call.WriteParam("action", "upload_avatar_game");
call.WriteParam("game", "true");
call.WriteParam("userID", userID.ToString());
call.WriteParam("file_data", Encoding.Default.GetString(data));
return call;
}
我在哪里进行网络电话
MemoryStream pngStream = new MemoryStream();
captureTarget.SaveToGLPng(pngStream);
var webCall = Tools.WebCall.UploadAvatar(pngStream.GetBuffer(),infoContext.UserID);
webCall.OnCallCompleted +=webCall_OnCallCompleted;
webCall.MakeCall();
pngStream.Close();
这一切都很好地到达了调用此方法的服务器
function upload_avatar_game($params){
$upload_dir = "../files/avatars/";
$data = $params['file_data'];
$dt = new DateTime();
$dtform = $dt->format("Y-m-d-H.i.s");
$name = $dtform . ".png";
$fullpath = $upload_dir . $name;
if(!isset($params['userID'])){
echo 'false';
die();
}
$user_id = $params['userID'];
try {
$existing_file = FileModel::get_user_avatar($user_id);
if($existing_file){
unlink($existing_file->getDownloadPath());
$existing_file->setIsDeleted(true);
$existing_file->Save();
}
$success = file_put_contents($fullpath,$data);
if($success){
$size = filesize($fullpath);
$file = FileModel::create_file($name,$upload_dir,0,$size,$user_id);
if($file){
$fid = $file->getFileID();
echo 'true';
die();
}
}
} catch(Exception $ex){
$GLOBALS['partial'] = '../partials/error.php';
$GLOBALS['error_msg'] = $ex->getMessage();
}
}
这里是参数被传递给方法的地方。正在测试它是否是合法请求,但没有对“file_data”条目进行任何操作
function process_script(){
if(!isset($_GET['action']) && !isset($_POST['action']))
{
header('Location: ?action=login_user');
}
$params = isset($_GET['action']) ? $_GET : $_POST;
//test for legimate request.
//after test
switch($params['action'])
{
case "upload_avatar_game":
if(isset($params['game']))
{
upload_avatar_game($params);
} else {
upload_avatar($params['fileUpload']);
}
break;
}
}
它被创建并添加到我的数据库中,但发送过来的数据都是乱码?我在这里转移是不是做错了什么?
谢谢 亚伦·斯图尔特
【问题讨论】:
-
请显示将
$params传递给upload_avatar_game的代码。WebCall是否默认发送POST请求? -
那它是怎么“乱码”的呢?
WebCall如何对传输进行编码?它可能需要在服务器端进行额外的解码。 -
call.WriteParam("file_data", Encoding.Default.GetString(data));这就是我编码它的方式。我不确定是否需要对其进行解码或其他什么?
-
stackoverflow.com/questions/10237983/… 你可以尝试传统的文件上传,而不是重新发明轮子
标签: c# php image encoding monogame