将图像发布到Facebook时指定图像大小

时间:2012-10-15 15:39:04

标签: c# facebook-graph-api facebook-c#-sdk

我正在使用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图像的大小,所以它不只是张贴图像的小缩略图?

1 个答案:

答案 0 :(得分: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;
 }