2

我正在使用 C# FaceBook API 将状态更新和图像发布到墙上。我正在使用以下代码:

    public string PostToFaceBookPage(string appKey, string appSecret, string wallId, string postMessage, string imageUrl)
    {
        string id = "";
        if (string.IsNullOrEmpty(imageUrl))
            imageUrl = "";
        var client = new Facebook.FacebookClient();

        dynamic result = client.Post("oauth/access_token",
                                      new
                                      {
                                          client_id = appKey,
                                          client_secret = appSecret,
                                          grant_type = "client_credentials",
                                          redirect_uri = "anywhere",
                                          scope = "publish_stream"
                                      });
        client.AccessToken = (result)["access_token"];

        if (!IsId(wallId))
        {
            result = client.Get(wallId);
            id = result["id"];
        } else
        {
            id = wallId;
        }

        if (imageUrl == "")
        {
            result = client.Post("/" + id + "/feed", new
                {
                    message = postMessage,
                    scope = "publish_stream",
                    privacy = "{\"value\": \"EVERYONE\"}"
                });
        } else
        {
            var uri = new Uri(imageUrl);
            string imageName = Path.GetFileName(uri.LocalPath);
            string mimeType = GetMimeType(Path.GetExtension(uri.LocalPath));
            var media = new Facebook.FacebookMediaObject
                {
                    FileName = imageName,
                    ContentType = mimeType
                };

            media.SetValue(GetImage(imageUrl));
            result = client.Post("/" + id + "/feed", new
            {
                message = postMessage,
                source = media,
                picture = imageUrl,
                scope = "publish_stream",
                privacy = "{\"value\": \"EVERYONE\"}"
            });
        }

        return "";
    }

一切正常。我唯一的问题是,无论实际图像的大小如何,我的图像都发布了相同的确切尺寸。有没有办法告诉 FaceBook 图像的大小,这样它就不会只发布图像的小缩略图?

4

1 回答 1

0

我不确定 Facebook API 是否可以拥有这些参数,因为我使用该 API 已经有几年了。

但是,作为临时修复,您可以尝试在将照片上传到 Facebook 之前调整其大小。

您可以使用来自http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing的以下代码

private static Image resizeImage(Image imgToResize, Size size)
{
   int sourceWidth = imgToResize.Width;
   int sourceHeight = imgToResize.Height;

   float nPercent = 0;
   float nPercentW = 0;
   float nPercentH = 0;

   nPercentW = ((float)size.Width / (float)sourceWidth);
   nPercentH = ((float)size.Height / (float)sourceHeight);

   if (nPercentH < nPercentW)
      nPercent = nPercentH;
   else
      nPercent = nPercentW;

   int destWidth = (int)(sourceWidth * nPercent);
   int destHeight = (int)(sourceHeight * nPercent);

   Bitmap b = new Bitmap(destWidth, destHeight);
   Graphics g = Graphics.FromImage((Image)b);
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;

   g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
   g.Dispose();

   return (Image)b;
 }
于 2012-10-15T15:53:24.633 回答